azurepowershellazure-devopsazure-rest-api

How am i supposed to add a field to the layout of a work item?


I try do add a field to a work item using this api : https://learn.microsoft.com/en-us/rest/api/azure/devops/processes/fields/add?view=azure-devops-rest-7.1&tabs=HTTP

It work (at least it did accept it, and return te good answer ) ! But the field does not appear in the layout of the work item in the UI, how am i supposed to do that ?


Solution

  • Thanks @Shamrai Aleksander, as mentioned by him, you need to add the custom field to one group. Besides, only inherited process supports customization. You can refer to the following PowerShell scripts.

    $token = "{PAT}"
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    $head = @{ Authorization =" Basic $token" }
    $orgname = "{Organization name}"
    $processId = "{Process id of your custom process}"
    $witRefName = "{The reference name of work item type}" #processname.workitemtype
    $groupid = "Agile.Task.Task.Planning" #The ID of the group to add the control to.
    
    # Add a field named 'CustomField' to work item type 'Task'
    $url1="https://dev.azure.com/$orgname/_apis/work/processes/$processId/workItemTypes/$witRefName/fields?api-version=5.0-preview.2"
    $body1 = @'
    {  
        “referenceName”: “Custom.CustomField”,  
        “type”: “string”,  
        “readOnly”: false,  
        “required”: false,  
        “allowGroups”: null  
    }  
    '@
    Invoke-RestMethod -Uri $url1 -Method Post -Headers $head -Body $body1 -ContentType application/json
    
    # Add custom field 'CustomField' to 'Planning' group
    $url2="https://dev.azure.com/$orgname/_apis/work/processes/$processId/workItemTypes/$witRefName/layout/groups/$groupid/controls?api-version=5.0-preview.1"
    
    $body2 = @'
    {  
        “order”:null,  
        “label”:“CustomField”,  
        “readOnly”:false,  
        “visible”:true,  
        “controlType”:null,  
        “id”:“Custom.CustomField" ,  
        “metadata”:null,  
        “inherited”:null,  
        “overridden”:null,  
        “watermark”:null,  
        “contribution”:null,  
        “height”:null,  
        “isContribution”:false  
    }  
    '@
    
    Invoke-RestMethod -Uri $url2 -Method POST -Headers $head -Body $body2 -ContentType application/json
    

    Then I can see it in Task's Planning group. enter image description here