зеркало из
1
0
Форкнуть 0

Sample to get all attachments on a specific work item (#160)

#147
This commit is contained in:
Dan Hellem 2018-08-22 09:21:15 -07:00 коммит произвёл GitHub
Родитель 562a886ec3
Коммит 64dfd8c7b1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 63 добавлений и 0 удалений

Просмотреть файл

@ -3,6 +3,7 @@ using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
@ -19,6 +20,67 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTracking
public class AttachmentsSample : ClientSample
{
[ClientSampleMethod]
public void GetAllAttachmentsOnWorkItem()
{
//this assumes you created a work item first from the WorkItemsSample.cs class
//if not, you can manually add a work item id
int workitemId = 0;
string fileFullPath = Path.GetTempFileName();
//check to see if the work item id is set in cache
try
{
workitemId = Convert.ToInt32(Context.GetValue<WorkItem>("$newWorkItem1").Id);
}
catch (Exception)
{
Console.WriteLine("No work item found in cache. Either create a new work item and attachments or manually set the id for known work item id.");
return;
}
VssConnection connection = Context.Connection;
WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();
WorkItem workitem = workItemTrackingClient.GetWorkItemAsync(workitemId, null, null, WorkItemExpand.Relations, null).Result;
if (workitem == null)
{
Console.WriteLine("No work item found for id");
return;
}
Console.WriteLine("Getting attachments on work item");
if (workitem.Relations == null || workitem.Relations.Count == 0)
{
Console.WriteLine("No attachments found on work item");
return;
}
foreach (var item in workitem.Relations)
{
if (item.Rel == "AttachedFile")
{
//string manipulation to get the guid off the end of the url
string[] splitString = item.Url.ToString().Split('/');
Guid attachmentId = new Guid(splitString[7].ToString());
Console.WriteLine("Getting attachment name and id: {0}", item.Attributes.GetValueOrDefault("name").ToString());
Stream attachmentStream = workItemTrackingClient.GetAttachmentContentAsync(attachmentId).Result;
using (FileStream writeStream = new FileStream(fileFullPath, FileMode.Create, FileAccess.ReadWrite))
{
attachmentStream.CopyTo(writeStream);
}
}
}
return;
}
[ClientSampleMethod]
public AttachmentReference UploadTextFile()
{
@ -94,5 +156,6 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTracking
return true;
}
}
}