With the new version of TFS on the horizon, which supports hierarchical links out of the box with a cool user interface to quickly create a nice work breakdown, many of us are still stuck to TFS 2008 (which of course has many great features :) ). This post shows you how you can create the links by making use of the TFS API to quickly simulate a breakdown. Especially when you combine this post with one of my previous posts Report on Work breakdown in TFS.
To make this example working, add the following using statements to your file
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
First start with the following code snippet to get the Team Project that contains the work items you want to link
var server = new TeamFoundationServer("myServer");
var store = (WorkItemStore)server.GetService(typeof(WorkItemStore));
var project = store.Projects["myTeamProject"];
then you have to find the ‘parent work item’ you want to add the work item links to (in the example the work item 1234)
var parentWI = store.GetWorkItem(1234);
This post will add all work items that are returned by a team query, so first we have to find the team query
private static StoredQuery GetTeamQuery(Project project, string queryName)
{
foreach (StoredQuery query in project.StoredQueries)
{
if (string.Compare(query.Name, queryName, true) == 0) return query;
}
return null;
}
Then of course we need to retrieve the query and make it executable by replacing all parameters in the query.
var query = GetTeamQuery(project, QueryTextbox.Text);
var querystring = query.QueryText.Replace("@project", "'" + project.Name + "'");
var wic = project.Store.Query(querystring);
Then iterate through the results of the query, and add all the work items to links of the ‘parent’ work item
foreach (WorkItem wi in wic)
{
parentWI.Links.Add(new RelatedLink(wi.Id));
}
Finally you have to save the 'parent' work item
parentWI.Save();
You can of course use this example as base for your solution to create some sort of auto-linkin of work items.
Good luck.