我正在尝试使用从Nuget下载的TeamFoundation / VisualStudio C#API以编程方式从VSTS中的用户故事中下载90多个附件。我试图使用这个例子:https://intellitect.com/downloading-attachments-from-tfs/
然而,该代码似乎已过时。我似乎无法找到文章中提到的这些确切的包:nuget-bot.Microsoft.TeamFoundation.Client 的NuGet-bot.Microsoft.TeamFoundation.WorkItemTracking.Client
但是,我下载了Microsoft.TeamFoundationServer.Client和Microsoft.TeamFoundationServer.ExtendedClient等TFS包,但WorkItem类似乎没有附件。有人知道我在哪里可以找到Attachments属性吗?我在Visual Studio中浏览了对象浏览器,但找不到它。或者,您是否可以建议另一种解决方案来获取工作项目中的附件?感谢。
答案 0 :(得分:2)
如果您还没有用于与API交互的访问令牌,请生成一个。
进行API调用以获取扩展的工作项信息。请注意$expand=all
参数以检索所有项目详细信息。
GET https://{account}.visualstudio.com/{project}/_apis/wit/workitems/115258?api-version=4.1&$expand=all
响应应该看起来像这样(如果项目有附件)。
{
"id": 115258,
"rev": 4,
"fields": {
"System.Id": 115258,
"System.AreaId": 2643
...and so on...
},
"relations": [
{
"rel": "AttachedFile",
"url": "https://{account}.visualstudio.com/d6c4b828-0f7e-4b69-a356-a92c0ec3cd07/_apis/wit/attachments/5682f031-4b09-478c-8042-0d2a998905e4",
"attributes": {
"authorizedDate": "2018-04-30T19:34:09.763Z",
"id": 2015371,
"resourceCreatedDate": "2018-04-30T19:34:07.873Z",
"resourceModifiedDate": "2018-04-30T19:32:16.057Z",
"revisedDate": "9999-01-01T00:00:00Z",
"resourceSize": 47104,
"name": "file.jpg"
}
}
]
}
对relations
rel
所在的AttachedFile
进行迭代,并致电url
以获取附件内容。
POST https://{account}.visualstudio.com/d6c4b828-0f7e-4b69-a356-a92c0ec3cd07/_apis/wit/attachments/5682f031-4b09-478c-8042-0d2a998905e4
来源:
答案 1 :(得分:2)
WebClient 无法正确向VSTS进行身份验证。您可以使用,而不是使用WebClient下载文件
double x = Canvas.GetLeft(myUserControl1);
方法WorkItemServer.DownloadFile()
下载 文件。有关详细信息,请参阅this thread。
您可以使用以下示例下载特定工作项的附件:
注意:安装nuget包Microsoft.TeamFoundationServer.ExtendedClient
Microsoft.TeamFoundation.WorkItemTracking.Proxy
然后,您可以尝试查询工作项并在每个循环中下载附件。有关详细信息,请参阅Fetch work items with queries programatically in VSTS。
以下示例供您参考:
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Proxy;
using System;
using System.IO;
namespace VSTS_DownloadWITAttachment
{
class Program
{
static void Main(string[] args)
{
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri("https://account.visualstudio.com/"));
ttpc.EnsureAuthenticated();
WorkItemStore wistore = ttpc.GetService<WorkItemStore>();
WorkItem wi = wistore.GetWorkItem(94);
WorkItemServer wiserver = ttpc.GetService<WorkItemServer>();
string tmppath = wiserver.DownloadFile(wi.Attachments[0].Id); //Change the number to download other attachments if there are more then one attachments for the specific work item. e.g: [1] to download the second one.
string filename = string.Format("D:\\WITAttachments\\{0}-{1}", wi.Fields["ID"].Value, wi.Attachments[0].Name);
File.Copy(tmppath, filename);
}
}
}