TFS API: Get list of associated work item from work item of any type

I have been observing most of users reported and looking for a way to list out associated work item linked to parent work item. Generally, when we associated work item to another work item - it simply attach it as Link irrespective link type.
We can achieve this using TFS Client Side Object model (API). Below is high level code snippet which will fetch all the links associated with work item.

// Connect to the server and the store. 

TfsTeamProjectCollection  teamProjectCollection = new TfsTeamProjectCollection(collectionUri);

//Get the work item service
workItemStore = (WorkItemStore)teamProjectCollection.GetService(typeof(WorkItemStore));

// Define a query that uses a variable for the type of work item. 
string queryString = string.Format("Select * From WorkItems where Id = {0}",workItemId);

// Create and run the query.
Query query = new Query(workItemStore, queryString);
WorkItemCollection results = query.RunQuery();

foreach(WorkItem wt in results)
{
       if (wt.Links.Count > 0)
       {
               LinkCollection links = wt.Links;
               foreach (Link link in links)
               {
                     //Add code to loop through all the links and use as needed
               }
        }

}


Hope this helps.

Comments

Popular posts from this blog

How To: Bulk upload Test Case results to Microsoft Test Manager

TFS API: Get Work Item History Revisions for parituclar Field

Add attachment to Test Case Result using TFS API