Perform CRUD Operations using Azure DevOps Rest API

Introduction

In modern agile development, effective handling of work items holds significant importance. This article delves into the automation of Azure DevOps Work Items through the utilization of PowerShell and the Azure DevOps REST API. This approach enables developers to optimize their processes, fostering improved workflow efficiency and collaboration. Additionally, we will explore various use cases and conclude with a demonstration.

Use Cases

Automating work items offers numerous advantages, such as:

  1. Efficiency: Rapidly create, update, and delete work items.
  2. Consistency: Guarantee uniformity in both creating and updating work items.
  3. Integration: Effortlessly incorporate work item management into existing scripts and pipelines.

Steps to perform CRUD operation

Prerequisite: Personal Access Token should used to perform CRUD operations.

Step 1. Create a new YAML pipeline and add a powershell script task with command snippets discussed below:

Declare variables as shown below.

#Authentication in Azure DevOps
#Replace these variables with your ADO organization URL and PAT

$ADOPAT = 'PERSONAL_ACCESS_TOKEN'
$OrgName = "Your_Organization"
$OrgURL = "https://dev.azure.com/$($OrgName)/"
$ADOAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($ADOPAT)")) }

Step 2. Create a work item. Required fields should be considered while creating the workitem. Below script with perform create operation.

#Create a work item

$WorkItemType = "task"
$WorkItemTitle = "Create Task"
$ProjectName = "ProjectName"; # replace with your project name

$uri = $OrgURL + $ProjectName + "/_apis/wit/workitems/$" + $WorkItemType + "?api-version=7.1"

$body=@"
[
  {
    "op": "add",
    "path": "/fields/System.Title",
    "value": "$($WorkItemTitle)"
  }
]
"@

Invoke-RestMethod -Uri $uri -Method POST -Headers $ADOAuthenicationHeader -ContentType "application/json-patch+json" -Body $body

Expected Result: A new workitem will be created with given title.

Step 3. Retrieve Work Item Using Id

The below script will retrieve work item details using workitem id.

# Get a work item by ID
$workItemId = 25 #replace with workitem id to be retrieved
$uri = $OrgURL + $ProjectName + "/_apis/wit/workitems/" + $workItemId + "?api-version=7.1"

Invoke-RestMethod -Uri $uri -Method GET -Headers $ADOAuthenicationHeader

Expected Result: Work Item Details will be visible in Pipeline run logs.

Step 4. Update Work Item

Below script will update work item by using work item id. Here we are updating title and description.

# Update a work item by ID
$title = "Updated Task"
$description = "This task has been updated."

$uri = $OrgURL + $ProjectName + "/_apis/wit/workitems/" + $workItemId + "?api-version=7.1"
$body = @"
[
    {
        "op": "replace",
        "path": "/fields/System.Title",
        "value": "$title"
    },
    {
        "op": "replace",
        "path": "/fields/System.Description",
        "value": "$description"
    }
]
"@

Invoke-RestMethod -Uri $uri -Method Patch -Headers $ADOAuthenicationHeader -ContentType "application/json-patch+json" -Body $body

Step 5. Delete Work Item

Below script is used to delete a workitem using ID.

# Delete a work item by ID
$uri = $OrgURL + $ProjectName + "/_apis/wit/workitems/" + $workItemId + "?api-version=7.1"

Invoke-RestMethod -Uri $uri -Method Delete -Headers $ADOAuthenicationHeader

Expected Result: The work item will be deleted from the current project.

Please find below complete YAML file for execution in Azure Pipeline.

trigger:
- main

pool:
  vmImage: windows-latest

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      #Authentication in Azure DevOps
      #Replace these variables with your ADO organization URL and PAT
      $ADOPAT = 'PERSONAL_ACCESS_TOKEN'
      $OrgName = "Your_Organization"
      $OrgURL = "https://dev.azure.com/$($OrgName)/"
      $ADOAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($ADOPAT)")) }
      
      #Create a work item
      
      $WorkItemType = "task"
      $WorkItemTitle = "Create Task"
      $ProjectName = "Project_Name"; #Replace with your project name
      
      $uri = $OrgURL + $ProjectName + "/_apis/wit/workitems/$" + $WorkItemType + "?api-version=7.1"
      
      $body=@"
      [
        {
          "op": "add",
          "path": "/fields/System.Title",
          "value": "$($WorkItemTitle)"
        }
      ]
      "@
      
      Invoke-RestMethod -Uri $uri -Method POST -Headers $ADOAuthenicationHeader -ContentType "application/json-patch+json" -Body $body
      
      # Get a work item by ID
      $workItemId = 28 # Replace with Work Item Id for which operation is to be performed
      $uri = $OrgURL + $ProjectName + "/_apis/wit/workitems/" + $workItemId + "?api-version=7.1"
      
      Invoke-RestMethod -Uri $uri -Method GET -Headers $ADOAuthenicationHeader
      
      # Update a work item by ID
      $title = "Updated Task"
      $description = "This task has been updated."
      
      $uri = $OrgURL + $ProjectName + "/_apis/wit/workitems/" + $workItemId + "?api-version=7.1"
      $body = @"
      [
          {
              "op": "replace",
              "path": "/fields/System.Title",
              "value": "$title"
          },
          {
              "op": "replace",
              "path": "/fields/System.Description",
              "value": "$description"
          }
      ]
      "@
      
      Invoke-RestMethod -Uri $uri -Method Patch -Headers $ADOAuthenicationHeader -ContentType "application/json-patch+json" -Body $body
      
      # Delete a work item by ID
      $uri = $OrgURL + $ProjectName + "/_apis/wit/workitems/" + $workItemId + "?api-version=7.1"
      
      Invoke-RestMethod -Uri $uri -Method Delete -Headers $ADOAuthenicationHeader

Conclusion

Automating Azure DevOps Work Items through PowerShell and the REST API offers a powerful solution for developers seeking to enhance their productivity and maintain consistency in project management. By following the steps outlined in this article, teams can efficiently manage work items, ensuring a smooth and streamlined development process. Please feel free to reach out in case any concerns.


Similar Articles