Get all work items from a project azure devops REST API
Asked Answered
R

4

13

I'm using Azure devops API to create a notification bot with AWS Lambda node.js. At this moment i need to check if each task work item is attached to a parent user story.

The first step will be to get all the task work items on "given" project, for this step i was reading azure devops api documentation and found this: Work Items - List

The API is asking for the id of the workitem that i want to get, but what if i need all the workitems from "given" project?

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

Or is there any other way to get all the workitem ids from a given project?

Riven answered 21/7, 2020 at 18:48 Comment(0)
H
13

You can use Work Items - Get Work Items Batch API that doesn't require ids but the maximum work items in the results are 200.

But, if you need to check Tasks, why you need get all the work items in the project? you can get only the Tasks, with Wiql - Query By Wiql API:

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

In the body make the query:

{
  "query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Task'"
}
Hildegardehildesheim answered 21/7, 2020 at 19:24 Comment(3)
How do you make this POST request? I am using Python and have a PAT...cannot figure this out. where will the PAT go?Foliate
Don't know about Python, but in Node, we pass PAT as an Authorization headerInkstand
The Work Items - Get Work Items Batch api now requires the ids parameter in the request body.Dacca
C
2

Furthering Shayki's response regarding using a WIQL query, I'd suggest that you can have two birds with one stone here by doing a work item links query that looks for Tasks without a parent User Story:

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

request payload:

{
  "query": "SELECT [System.Id] FROM workitemLinks WHERE ([Source].[System.WorkItemType] = 'Task') AND ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Reverse') AND ([Target].[System.WorkItemType] = 'User Story') MODE (DoesNotContain)"
}

This way, you won't have to loop through each work item to then check if it has a parent.

Note: the WIQL query limits the results returned to 20K and returns an error if the query results in more work items than that. Further reason to use a query like the above, if applicable.

Countermeasure answered 22/7, 2020 at 0:32 Comment(0)
E
2

There is a client for C#:

        public IEnumerable<int> GetIdList()
        {
            var connection = new VssConnection(new Uri("http://yourazuredevopsurl/"), new VssBasicCredential(string.Empty, personalAccessToken));
            var workItemTrackingHttpClient = connection.GetClient<WorkItemTrackingHttpClient>();
            var wiql = new Wiql
            {
                Query =  $@"Select [System.Id] From WorkItems Where [System.WorkItemType] = 'Test Case'"
            };
            
            var worItemsIds = workItemTrackingHttpClient.QueryByWiqlAsync(wiql, "Project name").Result;

            return worItemsIds.WorkItems.Select(reference => reference.Id);
Elswick answered 18/11, 2021 at 10:7 Comment(0)
H
-1

This works well - remember to declare the url, body and PAT variables first (you can use somthing like getpass for PAT):

r = requests.post(url, json=body,
    headers = {'Content-Type': 'application/json'},
    auth = ('', PAT))
print(r.content)
Habitat answered 15/3, 2023 at 12:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.