File attachment to a work item - TFS 2015

江枫思渺然 提交于 2019-12-13 03:39:11

问题


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:


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;



        }
    }
}


来源:https://stackoverflow.com/questions/47998600/file-attachment-to-a-work-item-tfs-2015

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!