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;
        }
    }
  • 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? – tehbeardedone Dec 27 '17 at 21:15

1 Answers1

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;



        }
    }
}
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39