简体   繁体   中英

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?

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 .

I'm using this documentation to do this.

With Wiql we can do queries to Azure Devops or TFS, I use Postman to work with it. The first step is use de following url: https://dev.azure.com/ {organization}/{projectId}/_apis/wit/wiql?api-version=5.0

Okey the following step is create a query by wiql, to this we will need to use json to send the query:

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

If the request it'a 200 Ok you will obtain a json with all Works Items.

My result: Result of my query

First of all, I am not using the python library, but I can tell you which APIs you have to use.

There is an API to retrieve all the work items . This is just an JSON object with all the work item types and properties. Be aware that this is limited to only 200 workitems each request. If you want more workitems, you have to write a WIQL query.

GET https://dev.azure.com/{organization}/{project}/_apis/wit/workitems?ids={ids}&api-version=5.0-preview.3

Personally I would like to advise you to use WIQL Queries to retrieve data from Azure DevOps. It is very flexible and it could be used in any situation.

Here you can find more information about WIQL queries

Here you can find the detailed information about the Azure DevOps Rest API for WIQL Queries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM