下载VSTS附件

时间:2018-06-28 07:47:55

标签: azure-devops azure-devops-rest-api

有人知道如何使用C#库从VSTS检索附件ID和下载WorkItem附件吗?我已经查看了nuget存储库中的AttachmentsSample,但是该示例未显示如何获取附件ID。它仅上传一个文件,然后转过来下载相同的文件。似乎没有任何地方记录C#API,并且VSTS REST API没有产生任何有用的信息。我快要死了,在这里!

1 个答案:

答案 0 :(得分:0)

您可以使用下面的代码示例下载工作项的附件:

安装Nuget软件包Microsoft.TeamFoundationServer.ExtendedClient

只需更改附件的存储路径,在下面的示例中,路径为D:\\temp\\vsts


using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Proxy;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;

namespace DownloadWITAttachments
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri uri = new Uri("https://account.visualstudio.com");
            string PAT = "nvxxxxxrdrtrxxxgghhjhvi3mia3yasldjfkoe353lew5pyywed";
            string project = "ProjectName";

            VssBasicCredential credentials = new VssBasicCredential("", PAT);

            //create a wiql object and build our query
            Wiql wiql = new Wiql()
            {
                Query = "Select * " +
                        "From WorkItems " +
                        "Where [Work Item Type] = 'User Story' " +
                        "And [System.TeamProject] = '" + project + "' " +
                        "And [System.State] <> 'Closed' " +
                        "And [System.AttachedFileCount] > 0 " +
                        "Order By [State] Asc, [Changed Date] Desc"
            };

            //create instance of work item tracking http client
            using (WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(uri, credentials))
            {
                //execute the query to get the list of work items in the results
                WorkItemQueryResult workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;

                if (workItemQueryResult.WorkItems.Count() != 0)
                {
                    //Download the first attachment for each work item.
                    foreach (var item in workItemQueryResult.WorkItems)
                    {
                        TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(uri);
                        ttpc.EnsureAuthenticated();
                        WorkItemStore wistore = ttpc.GetService<WorkItemStore>();
                        Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem wi = wistore.GetWorkItem(item.Id);
                        WorkItemServer wiserver = ttpc.GetService<WorkItemServer>();                       
                        string tmppath = wiserver.DownloadFile(wi.Attachments[0].Id);
                        string filename = string.Format("D:\\temp\\vsts\\{0}-{1}", wi.Fields["ID"].Value, wi.Attachments[0].Name);
                        File.Copy(tmppath, filename);
                    }
                }
            }
        }
    }
}