Link TFS Work Items Programmatically

One of the things that’s bothered me with TFS is the need to manually link Sprint Backlog Items to Product Backlog Items. During sprint planning meetings, we use Excel to quickly create lots of sprint backlog items for everybody on the team, but then we need to go through and manually create links to related product backlog items that we use for release planning and release progress tracking.

I decided to do a little research and found that TFS work items can be linked programmatically by utilizing the Visual Studio SDK’s TeamFoundation libraries. I use a TFPT command-line wiql query to find potential link candidates, then parse work item titles for a common number than can be used to find related work items. I do another wiql query to search for the work item ID of the related product backlog item. If found, I attempt to create a link between the two work items.

Here’s the relevant code:

TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer("mytfs");
WorkItemStore store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
 
WorkItem sbiWorkItem = store.GetWorkItem(sbiId);
 
try
{
    sbiWorkItem.Links.Add(new RelatedLink(pbiId));
    sbiWorkItem.Save();
    Console.WriteLine(string.Format("Linked SBI {0} to PBI {1}", sbiId, pbiId));
}
catch (Exception ex)
{
    // ignore "duplicate link" errors
    if (!ex.Message.StartsWith("TF26181"))
        Console.WriteLine("ex: " + ex.Message);
}

Resources:
Visual Studio 2008 SDK v1.1
http://download.microsoft.com/download/c/2/0/c20073e0-c842-44a8-a4e9-7dd5d289eafe/VsSDK_sfx.exe

Advertisement
%d bloggers like this: