Search code examples
azurepowershellazure-devopsazure-rest-api

How to add a field to a custom made page?


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 creates the page however despite having a the field declared in the workitem, it didn't place it but creates 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 ?


Solution

  • 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