le0
le0

Reputation: 851

VSTS / TFS REST API - Fetch Work Items and their linked ones

I am retrieving some User Stories using the the VSTS / TFS Web API and the code below:

var getWorkItemsHttpRequestMessage = new HttpRequestMessage(new HttpMethod("GET"), uri + "/_apis/wit/workitems?ids=736,731&&api-version=4.1");
var getWorkItemsHttpResponse = client.SendAsync(getWorkItemsHttpRequestMessage).Result;

if (getWorkItemsHttpResponse.IsSuccessStatusCode)
{    
    var workItems = getWorkItemsHttpResponse.Content.ReadAsAsync<HttpWorkItems>().Result;
    // ...

The query returns all the fields of the work items (user stories, in this case), but not the other items that is linked to them. I would like to retrieve the Tasks related to these User Stories.

How can it be done ? Is there another better way to do it ?

Upvotes: 2

Views: 500

Answers (1)

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41565

You can use the $expand parameter in the URL with the value relations:

https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/4?$expand=relations&api-version=4.1

In the results, you will get the work item links in the section relations:

"relations":[
 {
   "rel":"System.LinkTypes.Hierarchy-Forward",
   "url":"https://dev.azure.com/shaykia/_apis/wit/workItems/5",
   "attributes":{
   "isLocked":false
}

In the example above, we check work item 4 in the API, and in the results, we can see that work item 5 linked to him with a type of System.LinkTypes.Hierarchy-Forward that work item 4 the parent of 5 (5 he is the child, a task in this case).

You can read here about the relations types.

Upvotes: 3

Related Questions