Search code examples
azure-devops-rest-api

Adding a workitem as a child to a PBI


I want to add an existing workitem as a child of a PBI. Here is the powershell script that will be run as part of a release pipeline (classic pipeline)

# Define your Azure DevOps organization, project, and personal access token (PAT)
$organizationUrl = "https://dev.azure.com/ORG"
$organization = "ORG"
$projectName = "PROJ"

# Define the work item ID you want to retrieve
$workItemId = 14846
$accessToken = $env:SYSTEM_ACCESSTOKEN
$basicAuthValue = "Bearer $accessToken"
$headers = @{
    Authorization = $basicAuthValue
    "Content-Type" = "application/json-patch+json"
}
$headers
# Define the URL for the PATCH request
$apiUrl = "$organizationUrl/$projectName/_apis/wit/workitems/14846?api-version=7.0"
$apiUrl

# Define the JSON body for the PATCH request
$jsonBody = @(
    @{
        op = "test"
        path = "/rev"
        value = 46
    }
  @{
    op= "add"
    path= "/relations/-"
    value= @{
      rel= "System.LinkTypes.Dependency-Forward"
      url= "https://dev.azure.com/$organization/$projectName/_apis/wit/workItems/16170"
      attributes= @{
          isLocked = $false
          name = "Child"
      }
    }
  }
)

# Convert the JSON body to a string
$jsonString = $jsonBody | ConvertTo-Json -Depth 5
$jsonString

# Send the PATCH request
try {
    $response = Invoke-RestMethod -Uri $apiUrl -Headers $headers -Method Patch -Body $jsonString
   $response
    Write-Host "Work item updated successfully."
} catch {
    Write-Host "Error occurred: $($_.Exception.Message)"
    $_.Exception
}

The workitem is added to the PBI with relationship "Successor", not "Child". What did i do wrong here and how do i fix it?

I tried to use attributes but the resulting relationship is still "Successor" not "Child"


Solution

  • Update your string:

    rel= "System.LinkTypes.Dependency-Forward"
    

    to

    rel= "System.LinkTypes.Hierarchy-Forward"
    

    Here you can find all relationship types: The following command lists the work item link types in table format