I'm trying to figure out how to modify the parameter values in a test case in Azure Devops (ADO) using powershell. Unfortunately, the ADO REST API documentation doesn't go into much detail on that subject, so I've mostly been doing it through trial and error.
This is my code so far:
$PAT = "xxxxxxxxxxxxxxxxxxxxxxxx"
$Org = "MyOrganization"
$Project = "MyProject"
$ADOHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }
$BaseURI = "https://dev.azure.com/$($Org)/$($Project)/"
$Endpoint = $BaseURI + "_apis/wit/workitems/224572?api-version=6.0"
$result = Invoke-RestMethod -Uri $Endpoint -Method get -Headers $ADOHeader
$fields = $result.fields
[XML]$table = $fields.'Microsoft.VSTS.TCM.LocalDataSource'
$table.NewDataSet.Table1[0].Parameter1 = "209623"
$update = @{
"op" = "add"
"path" = "/fields/Microsoft.VSTS.TCM.LocalDataSource"
"value" = $table.OuterXml
} | ConvertTo-Json -Depth 5
$patch = Invoke-RestMethod -Uri $Endpoint -Method patch -Headers $ADOHeader -Body $update -ContentType "application/json-patch+json"
When I perform the get request in order to retrieve the test case data, the parameters are in the Microsoft.VSTS.TCM.LocalDataSource field as an XML object. I thought that since it's sent to me in XML format, then that's the format it needs to be sent back, but that doesn't appear to be the case because I keep receiving this error: "You must pass a valid patch document in the body of the request." I can't seem to track down the format that the patch document needs to be in.
Any help would be appreciated. Thank you!
edit - I added the c# tag incase someone has experience working with test cases in Azure Devops REST API through a c# project and knows how the patch document needs to be formatted.
Final Solution
Here is my modified code using Bright Ran-MSFT's recommendation
$PAT = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$Org = "MyOrganization"
$Project = "MyProject"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $PAT)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $base64AuthInfo))
$headers.Add("Content-Type", "application/json-patch+json")
$BaseURI = "https://dev.azure.com/$($Org)/$($Project)/"
$Endpoint = $BaseURI + "_apis/wit/workitems/{id}?api-version=6.1-preview.3"
$result = Invoke-RestMethod -Uri $Endpoint -Method get -Headers $headers
$table = $result.fields.'Microsoft.VSTS.TCM.LocalDataSource'
$table.NewDataSet.Table1[0].Parameter1 = "123456"
$pBody = $table.OuterXml.Replace('"','''')
$body = "[
{
`"op`": `"replace`",
`"path`": `"/fields/Microsoft.VSTS.TCM.LocalDataSource`",
`"value`": `"$pBody`"
}
]"
$put = Invoke-RestMethod -Uri $Endpoint -Method patch -Headers $headers -Body $body