【问题标题】:Download VSTS Attachments下载 VSTS 附件
【发布时间】:2018-06-28 17:45:03
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    安装 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);
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

    • @Matt 你解决了这个问题吗?代码对你有用吗?有更新吗?
    猜你喜欢
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 2018-06-18
    • 2018-10-08
    • 1970-01-01
    相关资源
    最近更新 更多