Get all work items from a project azure devops REST API - azure-devops

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?

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'"
}

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.

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);

Related

How can I get Azure Devops API to return the clauses of a query?

I'd like to get use the Azure DevOps API to get a specific query and modify it. My plan was to get the Get Query API, and add a new clause. However when I try to do this using the Azure DevOps C# SDK, I get null clauses. There is a read-only dictionary with a "wiql" url value, and when I load that up I see the results, not the query.
Is there a way for me to see the specific clauses in a query?
in C#, my code is specifically:
WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();
var query = await workItemTrackingClient.GetQueryAsync(c_projectName, "<queryIdGuid>");
If you want to get wiql, you have to use expand parameter. Example:
QueryHierarchyItem query = workItemTrackingClient.GetQueryAsync(c_projectName, queryPath, QueryExpand.Wiql).Result;
The base rest API: Queries - Get (QueryExpand)

How to get a list from all work items from a specific board in TFS API without ID?

I'm using TFS and I would like to get all work items from a specific board. This is how I'm getting the board atm: https://myTFSLink.com/tfs/TPC/ProjectName/MY-BOARD-NAME/_apis/work/boards?api-version=5.1
But nothing about stories/workItems are present in this API request. I know I can get all workItemsRevisions using
GET https://dev.azure.com/{organization}/{project}/_apis/wit/reporting/workitemrevisions?api-version=5.0
but I don't know how I can target this to a specific board :(
I also know that I can get multiple workItems by ID with
GET https://dev.azure.com/fabrikam/_apis/wit/workitems?ids=297,299,300&$expand=all&api-version=5.1
but the ideal would be get all workItems from a specific board as I said in the beginning. Is there any way to do it?
A specific board is corresponding to a Team/Area Path, so you can try to query the work items By Wiql. For example,
POST https://dev.azure.com/{organization}/{project}/{team}/_apis/wit/wiql?api-version=5.1
Request Body:
{
"query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'PRODUCT BACKLOG ITEM' AND [Area Path] = 'TestCase\\Team1' order by [System.CreatedDate] desc"
}

Get all items from iteration

I am trying to get all work items from a given iteration. I can filter on TaskType on other fields. But not iteration paths. Am I missing something here?
var body = "{\"query\": \"Select [System.Id], [System.Title], [System.State],[System.IterationPath] From WorkItems Where [System.IterationPath] = 'GAC\\Sprint 10'\"}";
var json = await HTTP.POST("https://xxx.visualstudio.com/_apis/wit/wiql?api-version=5.1", body, personalaccesstoken);
return JsonConvert.DeserializeObject<WorkItemIdList>(json);
I can filter on TaskType on other fields. But not iteration paths.
Does below scripts is what you want?
SELECT [System.Id], [System.Title], [System.WorkItemType], [System.State], [System.IterationPath] FROM workitems WHERE [System.IterationPath] = 'MerConsoleApp\Q4' AND [system.WorkItemType] = 'Bug'
This WIQL can filter the Bug work items which located IterationPath is MerConsoleApp\Q4.
In addition, I saw you are applying this WIQL from api. There's one thing you need pay attention, you may not get exactly work item data if you run WIQL from rest api.
For example, since we specify fields in select, here we can get below data format while we run this WIQL from UI:
But for rest api, we defined and fixed the data structure of the work item as id + url. This means you can only get the satisfied work item id and its url, even if you has specified the [System.Id], [System.Title], [System.WorkItemType], [System.State] in it.

How to get the linked items of a work item in Azure DevOps

I am working with the Azure APIs and the VisualStudio.Services.Client.
My goal is to get a list of test Cases associated with a User Story, so far I'm here:
VssConnection connection = new VssConnection(new Uri("https://{myOrg}.visualstudio.com"),
new VssBasicCredential("UserName","SuperSecretPassword"));
WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();
var ticket = witClient.GetWorkItemAsync(1234).Result;
Which returns me my user Story.
Question:
I want to get at any linked items to this to find my Test Cases, but can't see any way to do this through the UI (so i can call the query) or via the APIs directly.
Any help would be really appreciated!
Personally I like to use WIQL Queries to retrieve data from Azure DevOps. It is very flexible and it could be used in any situation.
This example below is made for an Azure DevOps Widget. (javascript)
var wiqlQuery = `
SELECT
[System.Id],
[System.Title],
[System.WorkItemType]
FROM workitemLinks
WHERE
(
[Source].[System.TeamProject] = #project
AND [Source].[System.WorkItemType] = 'User Story'
)
AND (
[System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward'
)
AND (
[Target].[System.TeamProject] = #project
AND [Target].[System.WorkItemType] = 'Test Case'
)
MODE (Recursive)`;
witClient.queryByWiql({ query: wiqlQuery }).then(function(result){
// Your Code
});
You should keep in mind that the LinkType in important in this query.
Here you can find detailed information about the relation types: Link type reference
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
If you have a query in Azure DevOps and you want to export it as WIQL query, you can use this marketplace extension

how to get all the Work item for a Iteration path by TFS API?

I am currently using TFS API who's link is https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#byids
currently, in my project I want to access all the Workitems who's iteration path= MRI_SCRUM_GIT http://apactfs.cbre.com:8080/tfs/CBRE.APAC.Applications/MRI_SCRUM_GIT/_workitems?_a=edit&id=61092
currently, i am finding a workitem number 6109 by this in postman GET
http://apactfs.cbre.com:8080/tfs/cbre.apac.applications/_apis/wit/workitems/61092?$expand=all&api-version=1.0
You need to execute a query to get all the Work item for a particular Iteration path. After executing a query, get the work items using the IDs that are returned in the query results response. You can get up to 200 work items at a time. Please refer to this article:
https://www.visualstudio.com/en-us/docs/integrate/api/wit/wiql#get-work-items
The sample is as below:
POST http://apactfs.cbre.com:8080/tfs/cbre.apac.applications/_apis/wit/wiql?api-version=1.0
Content-Type: application/json
{
"query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.IterationPath] under 'MRI_SCRUM_GIT'"
}