azure-devops-python-api query for work item where field == string

Viewed 22

I'm using the azure python api (https://github.com/microsoft/azure-devops-python-api) and I need to be able to query & find a specific work item based on a custom field value.

The closest thing I can find is the function create_query, but Im hoping to be able to run a query such as

queryRsp = wit_5_1_client.run_query(
    posted_query='',
    project=project.id, 
    query='Custom.RTCID=282739'
)

I just need to find my azure devops work item where the custom field RTCID has a certain specific unique value.

Do i need to create a query with the api, run it, get results, then delete the query? Or is there any way I can run this simple query and get the results using the azure devops api?

1 Answers

Your Requirement can be achieved.

For example, on my side, there is two workitems that have custom field 'RTCID':

enter image description here

enter image description here

The Below is how to use python to design this feature(On my side, both organization name and project name named 'BowmanCP'):

#query workitems from azure devops

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v5_1.work_item_tracking.models import Wiql
import pprint

# Fill in with your personal access token and org URL
personal_access_token = '<Your Personal Access Token>'
organization_url = 'https://dev.azure.com/BowmanCP'

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

# Get a client (the "core" client provides access to projects, teams, etc)
core_client = connection.clients.get_core_client()

#query workitems, custom field 'RTCID' has a certain specific unique value
work_item_tracking_client = connection.clients.get_work_item_tracking_client()
query  = "SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.Tags] FROM workitems WHERE [System.TeamProject] = 'BowmanCP' AND [Custom.RTCID] = 'xxx'"
#convert query str to wiql
wiql = Wiql(query=query)
query_results = work_item_tracking_client.query_by_wiql(wiql).work_items
#get the results via title
for item in query_results:
    work_item = work_item_tracking_client.get_work_item(item.id)
    pprint.pprint(work_item.fields['System.Title'])

Successfully got them on my side:

enter image description here

SDK source code is here:

https://github.com/microsoft/azure-devops-python-api/blob/451cade4c475482792cbe9e522c1fee32393139e/azure-devops/azure/devops/released/work_item_tracking/work_item_tracking_client.py#L704

You can refer to above source code.

Related