Search code examples
azurepowershellrestazure-devopsazure-devops-rest-api

How to hide a process's field from layout?


I try to create a powershell script to hide a process's field from the layout on Azure DevOps using this API : https://learn.microsoft.com/en-us/rest/api/azure/devops/processes/fields/remove-work-item-type-field?view=azure-devops-rest-7.1.

function hideWorkItemField {
    param ( $token, $organization, $processId, $witRefName, $fieldRefName )

    $headers = @{
        'Authorization' = "Bearer $token"
        'Content-Type' = 'application/json-patch+json'
    }
    $uri =  "https://dev.azure.com/$organization/_apis/work/processes/$processId/workItemTypes/$witRefName/fields/{0}?api-version=7.1" -f $fieldRefName
    Write-Host (Invoke-RestMethod -Uri $uri -Method Deletec -Headers $headers).value
}

if ( $args.Count -eq 6) {
    $token = $args[0]
    $auth = ([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($token)")))
    $organization = $args[1]
    $processId = getProcessId $auth $organization $args[2]
    $witRefName = getWorkItemReferenceName $auth $organization $processId $args[3]

    hideWorkItemField $token $organization $processId $witRefName (getWorkItemFieldReferenceName $auth $organization $processId $witRefName $args[4])
}

I receive 404 - page not found with my request but if i go to the url on my browser the "get" part seems to work, but i don't see any field that could be used to hide the field.

I can see the boolean on the layout of the workitem but i can't change it from there, and there is no obvious commande to do so... How am i supposed to hide a field with this api ?


Solution

  • Based on your description, the requirement is to hide a field from the layout of a certain work item type in a process, rather than to delete a field from layout. Therefore, we should not use the Fields - Remove Work Item Type Field - REST API (Azure DevOps Work Item Tracking Process) | Microsoft Learn, since to hide and to delete are two operations.

    Besides, you may have noticed that not all fields can be removed from the layout, but they could be hidden from the layout.

    enter image description here

    You may use this API to get the layouts of a work item type in a process; however as of now, I am afraid that there is no official API to hide a field from the layout.

    GET https://dev.azure.com/{organization}/_apis/work/processes/{processId}/workItemTypes/{witRefName}/layout?api-version=7.2-preview.1
    

    Sample response enter image description here

    For this, you may try the request captured from the browser developer tool (F12) when doing the same operation via the browser web UI.

    Here is the PowerShell script for your reference.

    $updateLayoutsURL = "https://dev.azure.com/$organization/_apis/work/processes/$processId/workItemTypes/AgileInherited.Task/layout/groups/Agile.Task.Task.Planning/Controls/Microsoft.VSTS.Common.Priority?api-version=5.0"
    
    $headers = @{
        'Authorization' = 'Bearer ' + "$token"
        'Content-Type' = 'application/json'
    }
    
    $body = @{
        "label" = "Priority"
        "visible" = $false
    } | ConvertTo-Json
    
    Invoke-RestMethod -Method Patch -Uri $updateLayoutsURL -Headers $headers -Body $body
    
    

    enter image description here