Joe Burkey
Joe Burkey

Reputation: 15

ADO: upload an attachment to Azure DevOps via HTTP REST request

We're using ADO Server 2019 and as part of a larger project I need to upload some emails to the ADO environment to attach to work items. I've already figured out that you first need to upload the attachment, pass over the attachment ID to the work item patch request and add the relationship from the work item end however what I can't for the life of me figure out is where to pick the actual email or any item up to upload.

The example that's given within the MS docs shows you how to post an upload but it uploads nothing, it just creates a blank page.

POST https://{instance}/fabrikam/_apis/wit/attachments?fileName=textAsFileAttachment.txt&api-version=5.0

A body for the request needs to be built to define where the attachment is from what I can gather however there isn't any kind of documentation around this for simply posting it via HTTP.

We're using Blue Prism so using C# is an option but not ideal.

Thanks in advance.

Upvotes: 1

Views: 6153

Answers (2)

Walter
Walter

Reputation: 3028

Please follow the steps below:

1.Upload a text file

POST https://{instance}/fabrikam/_apis/wit/attachments?fileName=textAsFileAttachment.txt&api-version=5.0

Request Body: "User text content to upload"

2.You will get a response like:

{
  "id": "6b2266bf-a155-4582-a475-ca4da68193ef",
  "url": "https://fabrikam:8080/tfs/_apis/wit/attachments/6b2266bf-a155-4582-a475-ca4da68193ef?fileName=textAsFileAttachment.txt"
}

Copy this url from the response.

3.Add an attachment to work items:

POST https://{instance}/fabrikam/_apis/wit/attachments?fileName=textAsFileAttachment.txt&api-version=5.0

Request body:

[
  {
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "AttachedFile",
      "url": "https://fabrikam:8080/tfs/_apis/wit/attachments/6b2266bf-a155-4582-a475-ca4da68193ef?fileName=textAsFileAttachment.txt",
      "attributes": {
        "comment": "Spec for the work"
      }
    }
  }
]

Replace the url of the request body.

Upvotes: 2

Shamrai Aleksander
Shamrai Aleksander

Reputation: 15988

You have to pass a file content if you create a new attachment. Upload a binary file

POST https://dev.azure.com/fabrikam/_apis/wit/attachments?fileName=imageAsFileAttachment.png&api-version=6.1-preview.3

"[BINARY FILE CONTENT]"

Here is an example through Powershell:

$user = ""
$token = "<personal access token>"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$org = "org_name"
$teamProject = "teamproject"
$wiId = "work item Id"

$folderPath = "c:/temp"
$fileName = "some_file.zip"

$createAttachmetUrlTemplate = "https://dev.azure.com/$org/$teamProject/_apis/wit/attachments?fileName={fileName}&api-version=5.0"
$updateWIUrlTemplate = "https://dev.azure.com/$org/_apis/wit/workitems/{id}?api-version=5.0"

$wiBodyTemplate = "[{`"op`": `"add`",`"path`": `"/relations/-`",`"value`": {`"rel`": `"AttachedFile`",`"url`": `"{attUrl}`", `"attributes`": {`"comment`": `"Spec for the work`"}}}]"

function InvokePostRequest ($PostUrl, $body)
{   
    return Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Body $body
}

function InvokePatchRequest ($PatchUrl, $body)
{   
    return Invoke-RestMethod -Uri $PatchUrl -Method Patch -ContentType "application/json-patch+json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Body $body
}

$bytes = [System.IO.File]::ReadAllBytes("$folderPath/$fileName")

$createAttachmetUrl = $createAttachmetUrlTemplate -replace "{filename}", $fileName

$resAtt = InvokePostRequest $createAttachmetUrl $bytes

$updateWIUrl = $updateWIUrlTemplate -replace "{id}", $wiId
$wiBody = $wiBodyTemplate -replace "{attUrl}", $resAtt.url

InvokePatchRequest $updateWIUrl $wiBody 

Upvotes: 1

Related Questions