Using this api i try to create a page with a custom field with this api : https://learn.microsoft.com/en-us/rest/api/azure/devops/processes/pages/add?view=azure-devops-rest-7.1&tabs=HTTP It create the page however dispite having a the field declarated in the workitem it didn’t place it but create a blank.
function addWorkItemFieldPage {
[CmdletBinding()] param (
[Parameter(Mandatory)] [string] $organization,
[Parameter(Mandatory)] [string] $processId,
[Parameter(Mandatory)] [string] $witRefName,
[Parameter(Mandatory)] [Object] $info,
[Parameter(Mandatory)] [hashtable] $header
)
[string] $uri = "https://dev.azure.com/{0}/_apis/work/processes/{1}/workItemTypes/{2}/layout/pages?{3}" -f $organization, $processId, $witRefName, $apiVersion
[string] $body = @{
sections = @{
id = "Section1"
groups = @{
id = $info.label
controls = @{
id = $info.refName
label = $info.label
order = $null
height = $null
visible = $true
readOnly = $false
metadata = $null
watermark = $null
inherited = $null
overridden = $null
controlType = $null
contribution = $null
isContribution = $false
}
overridden = $false
visible = $true
}
overridden = $false
visible = $true
}
label = $info.page
order = $null
overridden = $null
inherited = $null
visible = $true
locked = $false
pageType = 1
contribution = $null
} | ConvertTo-Json -Depth 3
Write-Host $uri
try {
Invoke-RestMethod -Uri $uri -Method Post -Headers $header -Body $body -ContentType application/json
} catch {
Get-Error
throw
}
}
What am i doing wrong ?
2
Answers
You might have more than one issue here.
It’s not clear where you are getting
$header
from in yourInvoke-WebRequest
. Ensure you have obtained a valid authentication token and set the header accordingly:Authorization: Bearer XXXXXXXXXXXXX..
I believe your nested object depth is 4, not 3. Just remove the
-depth 3
parameter and value fromConvertTo-Json
.You’ve made addWorkItemFieldPage a function with a cmdletBinding. It’s not clear though how it is being called. The function is expecting 5 parameters in order to insert the correct values in to the JSON body. You should be calling this function before invoking your rest method.
If all of the above is okay then check the values of
$info.label
,$info.refName
, and$info.page
. None of them should be empty or null.I tested the Pages – Add and got the same result as yours. I am not sure if there is something I missed in the request body.
As a workaround, I can use this Groups – Add REST API after creating the empty page to add the field in the page.
The note is that before adding the field, the field should exist in the workitem. Otherwise, it will return the error
"message":"TF51535: Cannot find field Custom.testfield."
For example, I had to add a field namedtestfield
in the workitem before I run the PowerShell script.The following is my test PowerShell script.
Test result: