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

Advertisements

6 thoughts on “Link TFS Work Items Programmatically”

    1. Those values are the Sprint Backlog Item ID & Product Backlog Item ID. That example relies on the user to provide them. You should be able to get them from the work items themselves or retrieve them programmatically.

  1. Attractive component of content. I simply stumbled upon your weblog
    and in accession capital to assert that I acquire actually
    loved account your weblog posts. Anyway I’ll be subscribing for your feeds and even I success you
    get entry to persistently rapidly.

  2. Stay fresh composition mate, you made it really salutary.
    You bring home the bacon believable information and data
    so we tooshie regulate objectively around their carrying out.

  3. Oh my goodness! Impressive article dude! Thank you,
    However I am having problems with your RSS. I don’t know why I can’t join it.

    Is there anyone else having similar RSS issues? Anybody who knows the answer will you kindly respond?
    Thanks!!

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s