skip to Main Content

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


  1. You might have more than one issue here.

    It’s not clear where you are getting $header from in your Invoke-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 from ConvertTo-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.

    Login or Signup to reply.
  2. 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 named testfield in the workitem before I run the PowerShell script.

    enter image description here

    The following is my test PowerShell script.

    # Replace the value with your actual values
    $organizationUrl = "https://dev.azure.com/organizationname"
    $personalAccessToken = ""
    $processesId=""
    $workItemType="basic1.Epic"
    
    $headers = @{
        Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
    }
    
    # API endpoint for create page 
    $pageUrl = "$organizationUrl/_apis/work/processes/$processesId/workItemTypes/$workItemType/layout/Pages?api-version=7.1-preview.1"
    
    $body = @"
    {
    "sections": [
        {
          "id": "Section1",
          "groups": [],
          "overridden": false
        },
        {
          "id": "Section2",
          "groups": [],
          "overridden": false
        },
        {
          "id": "Section3",
          "groups": [],
          "overridden": false
        }
      ],
      "id": "",
      "label": "newPage",
      "order": null,
      "overridden": null,
      "inherited": null,
      "visible": true,
      "locked": false,
      "pageType": 1,
      "contribution": null
    }
    "@
    
    try {
        $pageResponse = Invoke-RestMethod -Uri $pageUrl -Headers $headers -Method Post -Body $body -ContentType "application/json"
    #   $pageResponse | ConvertTo-Json
    } catch {
        Write-Host "Error: $_"
    }
    
    $pageID= $pageResponse.id
    
    # API endpoint for create group  
    $groupUrl = "$organizationUrl/_apis/work/processes/$processesId/workItemTypes/$workItemType/layout/pages/$pageId/sections/Section1/groups?api-version=7.1-preview.1"
    
    $groupbody = @"
    {
        "controls": [
            {
                "order": null,
                "label": "test field",
                "readOnly": false,
                "visible": true,
                "controlType": null,
                "id": "Custom.testfield",
                "metadata": null,
                "inherited": null,
                "overridden": null,
                "watermark": null,
                "contribution": null,
                "height": null,
                "isContribution": false
            }
        ],
      "id": null,
      "label": "NewGroup",
      "order": null,
      "overridden": false,
      "inherited": null,
      "visible": true
    }
    "@
    
    try {
        $groupResponse = Invoke-RestMethod -Uri $groupUrl -Headers $headers -Method Post -Body $groupbody -ContentType "application/json"
        $groupResponse | ConvertTo-Json
    } catch {
        Write-Host "Error: $_"
    }
    
    

    Test result:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search