trentR
trentR

Reputation: 13

Azure DevOps REST API parent relation link to existing work item error

I am attempting to utilize the Azure DevOps REST API to create and/or update a work item in Boards to allow adding a Parent link. So far this is unsuccessful. and its creating an error...

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"You must pass a valid patch document in the body of the request.","typeName":"Microsoft.VisualStudio.Services.Common.VssPropertyValidationException, Microsoft.VisualStudio.Services.Common","typeKey":"VssPropertyValidationException","errorCode":0,"eventId":3000}

Here is code I have tried.

# Define your organization, project, PAT (Personal Access Token), and work item IDs
$organization = "Org"
$project = "Test%20Project"
$token = "PAT"
$parentWorkItemId = "7787"
$childWorkItemId = "8678"

$ContentType = "application/json-patch+json"

# Construct the URI for the REST API call
$uri = "https://dev.azure.com/$organization/$project/_apis/wit/workitems/$childWorkItemId/?api-   version=7.2-preview.3"
echo $uri

# Create the JSON body for adding a parent link
$body = @"
[
    {
        "op": "add",
        "path": "/relations/-",
        "value": {
            "rel": "System.LinkTypes.Hierarchy-Reverse",
            "url": "https://dev.azure.com/$organization/_apis/wit/workItems/$parentWorkItemId"
        }
    }
 ]
"@

# Set the header with the PAT for authentication
$headers = @{
    Authorization = "Basic " +    [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($token)"))
 }

# Make the PATCH request to add the parent link
Invoke-RestMethod -Uri $uri -Method Patch -Body $body -ContentType $ContentType -Headers $headers

Upvotes: 1

Views: 183

Answers (1)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35564

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"You must pass a valid patch document in the body of the request.".

This issue is usually caused by the lack of [{}] in the Request body format. But in the PowerShell code, the request body should be correct.

The only issue I can see is that there are extra spaces in the Rest API url.

For example:

$uri = "https://dev.azure.com/$organization/$project/_apis/wit/workitems/$childWorkItemId/?api-   version=7.2-preview.3"

After I remove the additional spaces, the PowerShell script can work as expected.

Here is an example:

# Define your organization, project, PAT (Personal Access Token), and work item IDs
$organization = "ORG"
$project = "PROJECT"
$token = "PAT"
$parentWorkItemId = "1421"
$childWorkItemId = "1420"

$ContentType = "application/json-patch+json"

# Construct the URI for the REST API call
$uri = "https://dev.azure.com/$organization/$project/_apis/wit/workitems/$childWorkItemId/?api-version=7.2-preview.3"
echo $uri

# Create the JSON body for adding a parent link
$body = @"
[
    {
        "op": "add",
        "path": "/relations/-",
        "value": {
            "rel": "System.LinkTypes.Hierarchy-Reverse",
            "url": "https://dev.azure.com/$organization/_apis/wit/workItems/$parentWorkItemId"
        }
    }
 ]
"@

# Set the header with the PAT for authentication
$headers = @{
    Authorization = "Basic " +    [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($token)"))
 }

# Make the PATCH request to add the parent link
Invoke-RestMethod -Uri $uri -Method Patch -Body $body -ContentType $ContentType -Headers $headers

Result:

enter image description here

Upvotes: 0

Related Questions