gpt4 book ai didi

azure-devops - 下载 VSTS 附件

转载 作者:行者123 更新时间:2023-12-05 06:32:48 25 4
gpt4 key购买 nike

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

最佳答案

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

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

关于azure-devops - 下载 VSTS 附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51077454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com