Forum Discussion

surendra-p's avatar
surendra-p
Copper Contributor
Dec 06, 2024

azure board How get the work item parent work item id

I am using  work item details api _apis/wit/workItems/{workItemId}?api-version=7.1
my requirement is to get the parent id of the work item as per my investigation it says we have to check the relation [] but in response no suck kind of field.

i have verified that link is correctly set through UI and showing properly

  • 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}")

     

  • 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

    Work Items - Get Work Item - REST API (Azure DevOps Work Item Tracking) | Microsoft Learn

Resources