Im using n8n to create an integration Movidesk/AzureDevOps.
Im trying to get the attachments people put in the Movidesk to an AzureDevOps workitem, but cant figure out how to upload the attachment in the first place. The part of linkink it to a workitem I manage to succeded.
Since im using, i cant download anything in my local computer, so what i have to work with in this situation is just:
"attachments":[
{
"fileName":"minhaImagem.png",
"path":"7BDEA1B62A8641FF86982D0CF9F3DEC0",
"createdBy":{
"id":"CodRefDeQuemEnviouOArquivo",
"personType":1,
"profileType":1,
"businessName":"Nome de quem enviou o arquivo",
"email":"[email protected]",
"phone":"(47) 99999-9999"
},
"createdDate":"2017-05-29T14:19:50.0129141"
}
],
But i dont know how to use the “path” variable.
For
I used this post api call:
- POST https://dev.azure.com/%7BmyOrg%7D/%7BmyProject%7D/_apis/wit/attachments?fileName=test.xlsx&api-version=7.1-preview.3
It returns me an id and a url as it should, but when i open it it has no content.
I assumed it was a problem with the body of the API.Its missing something the content itself of the file, but i didnt figured it out how to use it with only the fileName and the path
in the work item, it stays like this. If i try downloading it, the file returns me an error that the file is corrupted
To upload an attachment to Azure DevOps and link the attachment to a work item via the REST API request, please follow the following steps:
- Convert file content to binary data. The sample PowerShell script:
$fileName = "123.png" # replace with your file name and file name extension
$filePath = "C:UsersxxxxPicturesScreenshots"+$fileName # replace with your file path
$fileContent = [System.IO.File]::ReadAllBytes($filePath)
- Upload the binary file to Azure DevOps as attachment.
POST https://dev.azure.com/xxxxx/_apis/wit/attachments?fileName=fileName.png&api-version=7.2-preview.4
You will get an id and a url as return. We will use the url from the response.
Example response:
{
"id": "a5cedde4-2dd5-4fcf-befe-fd0977dd3433",
"url": "https://dev.azure.com/xxxxx/_apis/wit/attachments/a5cedde4-2dd5-4fcf-befe-fd0977dd3433?fileName=fileName.png"
}
- Add an attachment to the workitem.
PATCH https://dev.azure.com/xxxxx/_apis/wit/workitems/{id}?api-version=7.2-preview.3
[
{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "AttachedFile",
"url": "https://dev.azure.com/xxxxx/_apis/wit/attachments/a5cedde4-2dd5-4fcf-befe-fd0977dd3433?fileName=fileName.png",
"attributes": {
"comment": "Attachment uploaded"
}
}
}
]
Here is the full sample PowerShell script:
$organization = ""
$project = ""
$personalAccessToken=""
$fileName = "123.png" # replace with your file name and file name extension
$filePath = "C:UsersxxxxPicturesScreenshots"+$fileName # replace with your file path
$workItemId = "583" # Replace with your work item ID
# Convert file content to binary data
$fileContent = [System.IO.File]::ReadAllBytes($filePath)
# Upload the file to Azure DevOps
$uri = "https://dev.azure.com/$organization/_apis/wit/attachments?fileName=$fileName&api-version=7.2-preview.4"
$headers = @{
Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$personalAccessToken"))
}
$response = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $fileContent -ContentType "application/octet-stream"
$attachmentUrl = $response.url
# Link the attachment to the work item
$uri = "https://dev.azure.com/$organization/_apis/wit/workitems/$($workItemId)?api-version=7.2-preview.3"
$body = @"
[
{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "AttachedFile",
"url": "$attachmentUrl",
"attributes": {
"comment": "Attachment uploaded via PowerShell script"
}
}
}
]
"@
$response = Invoke-RestMethod -Uri $uri -Method Patch -Headers $headers -Body $body -ContentType "application/json-patch+json"
Test result:
I can download these files and read them successfully.
1