1

I am looking to create a work Item in azure devops using API. I was able to create work item with title,description and area path, iteration path. Now I want to create a work item with title, description, area path, iteration path and attach a file and create a new work item.

There is API available for attaching a file after the work item is created but i want to attach a file first and create a work item

$Header = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }

$uri = "https://dev.azure.com/$organization/$project/_apis/wit/workitems/$"+"$WorkItemType"+"?api-version=6.0"

$body="[
  {
    `"op`": `"add`",
    `"path`": `"/fields/System.Title`",
    `"value`": `"$($WorkItemTitle)`"
  },
  {
    `"op`": `"add`",
    `"path`": `"/fields/System.Description`",
    `"value`": `"This is for workitme testing`"
  },
  {
    `"op`": `"add`",
    `"path`": `"/fields/System.AssignedTo`",
    `"value`": `"$($AssignUser)`"
  },
  {
    `"op`": `"add`",
    `"path`": `"/fields/System.AreaPath`",
    `"value`": `"$($AreaPath)`"
  },
  {
    `"op`": `"add`",
    `"path`": `"/fields/System.IterationPath`",
    `"value`": `"$($IterationPath)`"
  },
  {
    `"op`": `"add`",
    `"path`": `"/fields/System.AttachedFiles`",
    `"value`": `"spec.txt`"
  }
]"

Invoke-RestMethod -Uri $uri -Method POST -Headers $Header -ContentType "application/json-patch+json" -Body $body

i found this link can some one get this to powershell. I don't understand the body used for attachment url = attachment.Url link

$file = Get-ChildItem -Path "C:\Users\xx\Downloads\workitemAttachments\spec.txt"
$filename = $file.Name
$allFileBytes = [System.IO.File]::ReadAllBytes($file.FullName)
$body="[
    {
`"op`": `"add`",
Path = `"/relations/-`",
Value = new
{
`"rel`" = `"AttachedFile`",
`"url`" = `"$allFileBytes`",
`"attributes`" = `"{ `"comment`" = `"Comments for the file`"}`"
}
}
]"

$Header = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }
$type = "https://dev.azure.com/$organization/$project/_apis/wit/attachments?fileName=$filename&api-version=6.1-preview.3"
Invoke-RestMethod -Uri $type -Headers $Header -Method Post -Body $body -ContentType "application/json"

Based on the above link i tried to attach a file and I am getting errors

Sar
  • 219
  • 3
  • 20

2 Answers2

4

Try with below script:

$connectionToken="{PAT token}" \\Put PAT token
    
$file = Get-ChildItem -Path "C:\Users\xxx\Downloads\AusSouthEast.txt" \\Specify the file location
    
$url="https://dev.azure.com/{org name}/_apis/wit/attachments?fileName=AusSouthEast.txt&api-version=6.1-preview.3"
    
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
    
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -InFile $file -ContentType application/octet-stream

$AttachURL = $($response.url | ConvertTo-Json -Depth 100)
    
$CreateUrl="https://dev.azure.com/{org name}/{project name}/_apis/wit/workitems/`${wit type}?api-version=6.1-preview.3"
    
$WITBody=@"
[
  {
    "op": "add",
    "path": "/fields/System.Title", \\WIT title
    "from": null,
    "value": "From Powershell"
  },
  {
    "op": "add",
    "path": "/relations/-",  \\WIT attachment
    "value": {
      "rel": "AttachedFile",
      "url": $AttachURL,
      "attributes": {
        "comment": "Spec for the task"
      }
    }
  }
]
"@
    
Invoke-RestMethod -Uri $CreateUrl -Headers @{Authorization = "Basic $token"} -Method Post -Body $WITBody -ContentType application/json-patch+json

Tips:

  1. You don't need pre-read bytes of file. Just use -InFile in Invoke-RestMethod cmdlet to point to your local file, then specify the -ContentType application/octet-stream to indicate that the
    upload file contains binary data.

  2. To answer your second puzzle, "don't understand the body used for attachment url = attachment.Url". In fact, after you upload the attachments with api, all of them are managed by backend, and do not associated to any work items before you link it to wit. Also, this leads you could not view them from UI. The only way you find them is accessing them by using URL.

    After you upload the attachment successfully, as normal, you would see its stored URL from the response body. That is the one you can use to link to work item. You can refer to my previous answer for more explanation.

  3. When you following this api to create the work item with specific type, you need use $ + type name to point to the work item type. But $ is one of powershell special characters, you need use ` to escape the $.

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
0

You have to:

  1. Create a new attachment: Attachments - Create, Work Item attachment REST API 5.0 in Powershell
  2. Add an attachment to a work item: Work Items - Update - Add an attachment
Shamrai Aleksander
  • 13,096
  • 3
  • 24
  • 31
  • let's say I create a new attachment and after that can i create a new workitem with the parameters like workitem title,description and attachment? Am I getting it right? – Sar Sep 23 '20 at 16:53
  • @Sar I think, yes. You can create an attachment and use its url while creating a new work item. – Shamrai Aleksander Sep 23 '20 at 17:02
  • i found this link can some one get this to powershell. I don't understand the body used for attachment url = attachment.Url [link](https://stackoverflow.com/a/51671149/13102906) – Sar Sep 24 '20 at 02:33
  • @Sar When you create an attachment you receive a response with id and URL. You use this URL in a new work item body. – Shamrai Aleksander Sep 24 '20 at 05:53