Forum Discussion
azure board How get the work item parent work item id
- Dec 11, 2024
If you're not seeing the relations field in your response, check if the work item has the correct parent link set in the Azure DevOps UI plus your API request includes the $expand=relations parameter.
So modify api request to include the $expand parameter with the value relations:
GET https://dev.azure.com/{organization}/{project}/_apis/wit/workItems/{workItemId}?$expand=relations&api-version=7.1
https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/work-items/get-work-item?view=azure-devops-rest-7.1&tabs=HTTP
Here you are:
API Request
GET https://dev.azure.com/{organization}/{project}/_apis/wit/workItems/{workItemId}?$expand=relations&api-version=7.1
Example Request
GET https://dev.azure.com/myOrg/myProject/_apis/wit/workItems/12345?$expand=relations&api-version=7.1
Example Response
In the response, you should see a relations array. Look for an entry with the rel attribute set to "System.LinkTypes.Hierarchy-Reverse" which indicates a parent link.
{
"id": 12345,
"relations": [
{
"rel": "System.LinkTypes.Hierarchy-Reverse",
"url": "https://dev.azure.com/myOrg/_apis/wit/workItems/67890",
"attributes": {
"isLocked": false
}
}
]
}
Extracting the Parent ID
To extract the parent ID, you can parse the url field in the relations array. In the example above, the parent ID is 67890.
Sample Code to Extract Parent ID
Here’s a sample code snippet in Python to demonstrate how you can extract the parent ID:
import requests
# Replace with your actual values
organization = "myOrg"
project = "myProject"
work_item_id = 12345
api_version = "7.1"
url = f"https://dev.azure.com/{organization}/{project}/_apis/wit/workItems/{work_item_id}?$expand=relations&api-version={api_version}"
response = requests.get(url)
data = response.json()
parent_id = None
for relation in data.get("relations", []):
if relation["rel"] == "System.LinkTypes.Hierarchy-Reverse":
parent_url = relation["url"]
parent_id = parent_url.split("/")[-1]
break
print(f"Parent ID: {parent_id}")