CThomas
CThomas

Reputation: 71

Functionality of Attachments and their use in Work Item Tracking API

I stumbled across the documentation for creating attachments, listed under work item tracking, and I'm curious about the functionality. According to this stack overflow post, the functionality seems to be that it uploads a file to the "backend" without any association to a work item. I am having some trouble understanding what the use case for this endpoint is... Is it just a glorified storage space? What is its intended use if there is no UI to view the item and no relations to any existing entities other than a project? Is there any available end point that I am missing that would allow me to add an attachment to an existing work item?

Upvotes: 0

Views: 424

Answers (1)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30313

You also need to use Work Items - Update rest api to add the attachments to a work item. See below:

1, First use Attachments - Create rest api to update the attachment to azure devops server.

When the attachment is successfully uploaded, The attachment id and url will be returned to you. And you will need to use the attachment url in the Work Items - Update rest api. See examples

2, Use Work Items - Update to add the attachment to the work item. Put the attachment url in the request body like below example:

[{
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "AttachedFile",
      # attachment url
      "url": "https://dev.azure.com/fabrikam/_apis/wit/attachments/098a279a-60b9-40a8-868b-b7fd00c0a439?fileName=Spec.txt",
      "attributes": {
        "comment": "Spec for the work"
      }
    }
  }]

See below full example in powershell scipts:

# Attachments - Create REST API
$url = "https://dev.azure.com/{org}/{proj}/_apis/wit/attachments?fileName=textAsFileAttachment.txt&api-version=6.0"

$PAT="Personal access token"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))

$body= 'this a attached text file'

# update attachment to azure devops server. attachment id and url will be returned
$attachment= Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -ContentType "application/octet-stream" -Method post -Body $body

# Work Items - Update REST API
$wurl ="https://dev.azure.com/{org}/{proj}/_apis/wit/workitems/{workitem Id}?api-version=6.0"

$wbody=@(
   @{
    "op"= "add";
    "path"= "/relations/-";
    "value"= @{
      "rel"= "AttachedFile";
      "url"= $attachment.url;  #Attachment url
      "attributes"= @{
        "comment"= "attachment Test"
      }
    }
  })
  # add attachment to workitem.
  Invoke-RestMethod -Uri $wurl -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -ContentType "application/json-patch+json" -Method patch -Body (convertto-json $wbody -Depth 10)

Then the attachment you uploaded to azure devops will be attached to a workitem. See below result of above example:

enter image description here

Upvotes: 1

Related Questions