Nick
Nick

Reputation: 850

How to Add attachments to an existing work item in Azure DevOps using c#

Would you please provide a code sample on how to add attachments (text file and an image) to an existing work item in Azure DevOps using C#.

Upvotes: 0

Views: 9857

Answers (1)

Hugh Lin
Hugh Lin

Reputation: 19361

Here are some blog and cases: 1 . 2 with C# code samples, you can refer to them.

using (FileStream attStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
{
    var att = WitClient.CreateAttachmentAsync(attStream, filePathSplit[filePathSplit.Length – 1]).Result; // upload the file
    
    JsonPatchDocument patchDocument = new JsonPatchDocument();
    
    patchDocument.Add(new JsonPatchOperation()
    {
        Operation = Operation.Add,
        Path = "/relations/-",
        Value = new
        {
            rel = "AttachedFile",
            url = att.Url,
            attributes = new { comment = “Comments for the file “ + filePathSplit[filePathSplit.Length – 1] }
        }
    });

    var workItem = UpdateWorkItemAsync(patchDocument, workItemId).Result;
}

Upvotes: 7

Related Questions