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