2
votes

I am trying to get the list of the attachments against my workitem along with that I need, attachment count, attachments name. which are attached to my workitem. I tried to read the documentation of Azure DevOps I can see the following

https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/attachments?view=azure-devops-rest-5.0

It has Get, Create and List endpoints available. But to get it asks for the attachment ID which not available because no endpoint returns the attachment details.

Can you please guide me which API endpoint I can use to get the attachment details of a worktiem.

1

1 Answers

2
votes

You should first to get the work item details with Work Items - Get Work Item Rest API:

GET https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}

Note: to get the attachments details you need to add this to parameter the url:

$expand=all

Now in the results you will get the relations property, there you will find the attachments url, in the url you can find the id.

For example:

$url = https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/434?$expand=all&api-version=5.0

$workItem = Invoke-RestMethod -Uri $url -Method Get -ContentType application/json

$split = ($workitem.relations.url).Split('/')

$attachmentId = $split[$split.count - 1]

# Result: 1244nhsfs-ff3f-25gg-j64t-fahs23vfs

Now you can use the attachment api to download the attachment.