skip to Main Content

Does azure devops support the retrieval of data from rest API and populate that data in the workitem fields

I tried writing powershell script to retrieve data from rest API and populate that data in workitem fields in azure devops

2

Answers


  1. Yes, you can get the fields of workItems by calling the API below: –

    GET API: –

    Reference1

    GET https://dev.azure.com/organization/_apis/work/processes/processId/workItemTypes/witRefName/fields/fieldRefName?api-version=7.1-preview.2
    
    $PAT = "xxxxxxxxxxxxx3ceq"
    $OrgName = "sid24desai0738"
    $ProjectName = "AzureDevops"
    $ApiVersion = "7.0"
    $processId =  ""
    $witRefName = ""
    $fieldRefName = ""
    
    
    $services = Invoke-RestMethod -Uri "https://dev.azure.com/$OrgName/_apis/work/processes/$processId/workItemTypes/$witRefName/fields/$fieldRefName?api-version=7.1-preview.2" -Method Get -Headers @{Authorization=("Basic {0}" -f [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$PAT")))}
    
    $services.value
    

    And update the Fields by calling the API below: –

    Update API:-

    Reference2

    PATCH https://dev.azure.com/organization/_apis/work/processes/processId/workItemTypes/witRefName/fields/fieldRefName?api-version=7.1-preview.2
    
    $personalAccessToken = "YOUR-PAT-GOES-HERE"
    $organization = "YOUR-ORGANIZATION-NAME"
    $processId = "PROCESS-ID"
    $witRefName = "WORK-ITEM-TYPE-REF-NAME"
    $fieldRefName = "FIELD-REF-NAME"
    $apiVersion = "7.1-preview.2"
    
    $baseUrl = "https://dev.azure.com/$organization/_apis/work/processes/$processId/workItemTypes/$witRefName/fields/$fieldRefName?api-version=$apiVersion"
    
    $body = @{
        defaultValue = "Blue"
    } | ConvertTo-Json
    
    $headers = @{
        Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
        "Content-Type" = "application/json"
    }
    
    try {
        $response = Invoke-RestMethod -Uri $baseUrl -Method PATCH -Headers $headers -Body $body -ErrorAction Stop
        Write-Output "Field default value updated successfully."
    } 
    catch {
        Write-Error $_.Exception.Message
    }
    

    There’s one more API to update a particular field in WorkItem

    Reference3 :-

    PATCH https://dev.azure.com/{organization}/_apis/work/processes/{processId}/workItemTypes/{witRefName}/fields/{fieldRefName}?api-version=7.1-preview.2
    
    $PAT = "xxxxxxxxxsxq"
    $OrgName = "sid24desai0738"
    $ApiVersion = "7.1-preview.3"
    $WorkItemId = "123"  # Replace with the actual work item ID
    
    $uri = "https://dev.azure.com/$OrgName/_apis/wit/workitems/$WorkItemId?api-version=$ApiVersion"
    
    $body = @(
        @{
            op = "test"
            path = "/rev"
            value = 1
        },
        @{
            op = "add"
            path = "/fields/System.AreaPath"
            value = "Fabrikam-Fiber-Git\Website"
        },
        @{
            op = "add"
            path = "/fields/System.History"
            value = "Moving to the right area path"
        }
    ) | ConvertTo-Json
    
    Invoke-RestMethod -Uri $uri -Method Patch -Headers @{Authorization=("Basic {0}" -f [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$PAT")))} -ContentType "application/json-patch+json" -Body $body
    
    
    Login or Signup to reply.
  2. After you retrieve the value from 3rd party rest api, you can parse the response to get the value wanted, then set the value to the field value in rest api below to update the work item.

    The $fieldvalue is gotten from your 3rd party rest api as an example. The reference doc: Update a field.

    $org = "orgname"
    $witId = "workitemid"
    $fieldvalue = "Active"     # This is the value got from 3rd party rest api.
    
    $token = "PAT"
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    $url="https://dev.azure.com/$org/_apis/wit/workitems/$witId" + "?api-version=7.0"
    echo $url
    
    $body = @(
      @{
        op = "add"
        path = "/fields/System.State"        # the field path
        value = "$fieldvalue"                # the field value
       }
    )
    
    $head = @{ Authorization =" Basic $token" }
    $response = Invoke-RestMethod -Uri $url -Method PATCH -Headers $head -Body (ConvertTo-Json -InputObject $body) -ContentType application/json-patch+json
    

    Please make sure your account (which generated the PAT) has enough permission to edit the boards work item, you can simply set the account as contributor rule of the DevOps project.
    enter image description here

    does azure devops support dynamic retrieval of value from 3rd party rest API

    To dynamically retrieve value from 3rd party rest api, you can consider to use DevOps pipeline, and could have options below:

    1. You could need to monitor the change of the 3rd party rest api somehow, it depends on your 3rd party rest api, and then trigger devops pipeline, in pipeline you can get the value and update the work item value.

    2. Or you can set schedule trigger on DevOps pipeline, so that the pipeline will automatically run in schedule to update the work item.

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