Reputation: 719
I want to download Work Item details from our Azure DevOps Services instance and push it into our local TFS Server.
I got as far as connecting to the DevOps server using the WorkItemTrackingHttpClient in C#. I can iterate through the work items i find and can see the fields, download attachments using GetAttachmentContentAsync. So far all good.
The last detail i need to complete this is to also copy/download the embedded images that are in the HTML Description fields. I can extract the HTML code from the HTML field and see one or more img src lines in it.
<img src=\"https://dev.azure.com/tenant/projectguid/_apis/wit/attachments/d928c8ee-f493-4d30-99a5-f62b7f36a2f7?fileName=grafik.png"\>
All of these have the fileName grafik.png, but a different fileGuid.
I tried to use the GetAttachmentContentAsync to download on this guid, but that did not work. I tried to download the file using a normal Stream, but that didnt work, i assume this is due to authentication. The image files also do not show up as a normal attachment.
So the question is, how can i download this image from Azure DevOps from my C# client, which is authenticated using a PAT through the API. Is there an API call i can use to download this file ? i have not been able to find it, or can i somehow authenticate the WebClient with that PAT so that it can download it ?
Upvotes: 3
Views: 1844
Reputation: 719
After further troubleshooting, now the GetAttachmentContentAsync is able to download the image.
For anyone what might be looking for a similar solution. This is what i did
Code used to download
public string DownloadAttachment(string url, string fileName, int workItemId)
{
string[] urlSplit = url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
int index = urlSplit.Length - 1;
Guid attGuid = new Guid(urlSplit[(index)]);
if (fileName == string.Empty)
{
fileName = urlSplit[(index)] + ".png";
}
return DownloadAttachment(attGuid, fileName, workItemId);
}
public string DownloadAttachment(Guid attGuid, string fileName, int workItemId)
{
var credentials = new VssBasicCredential(string.Empty, this.personalAccessToken);
// create instance of work item tracking http client
using (var httpClient = new WorkItemTrackingHttpClient(this.uri, credentials))
{
Stream attStream = httpClient.GetAttachmentContentAsync(attGuid).Result;
string folderPath = @"C:\Temp\WorkItemAttachments\" + workItemId;
string fileFullPath = folderPath + "\\" + fileName;
Directory.CreateDirectory(folderPath);
using (FileStream writeStream = new FileStream(fileFullPath, FileMode.Create, FileAccess.ReadWrite))
{
attStream.CopyTo(writeStream);
}
}
return fileName;
}
Upvotes: 2