Search code examples
azure-devopsazure-devops-rest-api

getting linked workitem in azure devops using python


I'm using Azure Devops module of python to get the linked workitems to the userstory in Tree structure(parent,child link)

I tried this code

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v7_1.work_item_tracking.models import Wiql
from variable import personal_access_token,org,project

# Replace with your personal access token and organization URL
personal_access_token = personal_access_token
organization_url = org
project_name = project

# Create a connection to the Azure DevOps organization
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

# Get the work item tracking client
wit_client = connection.clients.get_work_item_tracking_client()

# Define a WIQL (Work Item Query Language) query to get work items
wiql_query = Wiql(
    query=f"""
    SELECT
        [System.Id],
        [System.WorkItemType],
        [System.Title],
        [System.AssignedTo],
        [System.State],
        [System.Tags]
    FROM workitems
    WHERE
        (
            [Source].[System.TeamProject] = 'DevOps RnD'
            AND [Source].[System.Id] = 326797
        )
    AND (
            [System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward'
        )
    ORDER BY [System.Id]
    MODE (Recursive)
    """
)

wiql_result = wit_client.query_by_wiql(wiql=wiql_query)

but this gives error on this line

wiql_result = wit_client.query_by_wiql(wiql=wiql_query)

saying

Exception has occurred: AzureDevOpsServiceError The MODE keyword can not be used inside work item query. The error is caused by «Recursive».

Question: How do I resolve this Error?

Any help would be really appreciated!


Solution

  • Test the same Python sample and I can reproduce the same issue.

    The cause of the issue could be that the Wiql() doesn't support Mode keyword in wiql definition.

    To use the wiql format in your python sample, you can use the Rest API: Wiql - Query By Wiql in the Python script to get the work items.

    POST https://dev.azure.com/{organization}/{project}/_apis/wit/wiql?api-version=6.0
    

    Here is an example:

    import requests
    from requests.auth import HTTPBasicAuth
    
    
    personal_access_token = 'PAT'
    organization_url = 'https://dev.azure.com/OrganizationName'
    project = 'ProjectName'
    parent_work_item_id = 326797  # Replace with your parent work item ID
    
    
    wiql_url = f'{organization_url}/{project}/_apis/wit/wiql?api-version=6.0'
    
    
    wiql_query = {
        "query": f"""
        SELECT
            [System.Id],
            [System.WorkItemType],
            [System.Title]
        FROM workitemLinks
        WHERE
            (    
                [Source].[System.TeamProject] = 'DevOps RnD'
                And [Source].[System.Id] = {parent_work_item_id}
            )
            AND (
                [System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward'
            )
         ORDER BY [System.Id]
         MODE (Recursive)
        """
    }
    
    
    response = requests.post(wiql_url, json=wiql_query, auth=HTTPBasicAuth('', personal_access_token))
    response.raise_for_status()
    wiql_results = response.json()
    
    print(f"{wiql_results }")
    

    Result:

    enter image description here

    It will return System.LinkTypes.Hierarchy-Forward type linked work item. The Source -> ID is Parent work item id and Target -> ID is Child work item ID.