1

I am trying to attach a file with TFS test case work item. As first step, I am trying to create the attachment file in TFS attachment store. Once the file is created in the attachment store, I would be getting AttachmentReference object and with that object, I am planning to attach the file with the selected work item with ID 595. But, my process hangs at CreateAttachmentAsync function call. Any help is appreciated!

 public void AttachFile(VssConnection connection) { //use the workItemTrackingHttpClient try { using (WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>()) { //create a work item //WorkItem wi = witClient.GetWorkItemAsync(595, expand: WorkItemExpand.All).Result; string filePath = @"C:\Temp\attach-file.PNG"; AttachmentReference attachRef = witClient.CreateAttachmentAsync(filePath, "simple").Result; JsonPatchDocument patchDocument = new JsonPatchDocument(); //add fields to your patch document patchDocument.Add( new JsonPatchOperation() { Operation = Operation.Add, Path = "/relations/-", Value = new { rel = "AttachedFile", url = attachRef.Url, attributes = new { comment = "Adding new attachment for Test Case 2" } } } ); WorkItem result = witClient.UpdateWorkItemAsync(patchDocument, 595).Result; } } catch (Exception ex) { string msg = ex.Message; } } 
1
  • The default upload type is already set to simple. You shouldn't need to pass it in. Also, that value should be an enum property. Could this be what's hanging you up? Commented Dec 27, 2017 at 21:15

1 Answer 1

2

Try to remove "simple" in AttachmentReference attachRef = witClient.CreateAttachmentAsync(filePath, "simple").Result. Please try the code below:

using Microsoft.TeamFoundation.WorkItemTracking.WebApi; using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models; using Microsoft.VisualStudio.Services.Client; using Microsoft.VisualStudio.Services.WebApi.Patch; using Microsoft.VisualStudio.Services.WebApi.Patch.Json; using System; namespace UploadAttachment { class Program { static void Main(string[] args) { // Full path to the binary file to upload as an attachment string filePath = @"C:\Temp\attach-file.PNG"; var myCredentials = new VssClientCredentials(); var connection = new VssConnection(new Uri(@"http://tfsserver:8080/tfs/TeamProjectCollectionName"), myCredentials); WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>(); AttachmentReference attachment = workItemTrackingClient.CreateAttachmentAsync(filePath).Result; JsonPatchDocument patchDocument = new JsonPatchDocument(); patchDocument.Add( new JsonPatchOperation() { Operation = Operation.Add, Path = "/relations/-", Value = new { rel = "AttachedFile", url = attachment.Url, attributes = new { comment = "Adding new attachment for Test Case" } } } ); WorkItem result = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, 595).Result; } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

The code works if it is a console application, even with "simple". my code is written in windows form and the window gets hanged when creating the attachment
@SwarupPanja You may try to create a new thread to see whether the issue persists.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.