'How to get all work items(Epics, Features, Issue, Task, Test Case, User Story, etc) for an Azure Devops project?

I am trying to fetch all the work items(Epics, Features, Issue, Task, Test Case, User Story, etc) and then classify them for a given project using Microsoft's azure devops python api(aka vsts) library.

In the work_item_tracking I am unable to find any function to get all work item or fetch all work item on the basis of it's type.

Is there already a function for getting all work items that I am unable to find or should I write a WIQL query to fetch the required data?



Solution 1:[1]

I'm following this documentation to implement this.

With Wiql we can do queries to the new version Azure Devops or TFS. In order to test it out I'm using Postman to consume the services before implement it. The first step is use de following url:

https://dev.azure.com/{organization}/{projectId}/_apis/wit/wiql?api-version=5.0

You must do a Post request to the URL using the following json body to make the the query. Check below the json payload.

{
  "query": "Select [System.Id], [System.Title], [System.State], [System.WorkItemType] From WorkItems"
}

Finally if the request is good you will receive a 200 HTTP response with the content of the query, if in the other hand you receive an error the request will rise the erro description and the HTTP code.

Check my response in the following link and check the data structure that is returning the request: Result of my query

Solution 2:[2]

Is there already a function for getting all work items that I am unable to find or should I write a WIQL query to fetch the required data?

You are right. We could use write a WIQL query to fetch the System Ids, then we could according system.Ids to query the workitems. The following is the demo code to fetch all of the workitems with python code.

from vsts.vss_connection import VssConnection
from msrest.authentication import BasicAuthentication
import json
from vsts.work_item_tracking.v4_1.models.wiql import Wiql

def emit(msg, *args):
print(msg % args)

def print_work_item(work_item):
    emit(
        "{0} {1}: {2}".format(
            work_item.fields["System.WorkItemType"],
            work_item.id,
            work_item.fields["System.Title"],
        )
    )

personal_access_token = 'YourPATToken'
organization_url = 'https://dev.azure.com/YourorgName'
# Create a connection to the org
credentials = BasicAuthentication('', personal_access_token)
connection = VssConnection(base_url=organization_url, creds=credentials)
wiql = Wiql(
        query="""select [System.Id] From WorkItems """
    )

wit_client = connection.get_client('vsts.work_item_tracking.v4_1.work_item_tracking_client.WorkItemTrackingClient')
wiql_results = wit_client.query_by_wiql(wiql).work_items
if wiql_results:
        # WIQL query gives a WorkItemReference with ID only
        # => we get the corresponding WorkItem from id
        work_items = (
            wit_client.get_work_item(int(res.id)) for res in wiql_results
        )
        for work_item in work_items:
            print_work_item(work_item)

For more demo code, you could refer to this link.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2