TFS SDK: Getting Started

TFS is great, but it simply doesn’t do everything I need it to do. My team has a TFS work-item-driven software development life-cycle that depends on having stories organized with tasks of certain activity types performed in a specific order. It’s a difficult process for new team members to internalize, and it requires discipline for even our veteran team members. One of the things that I’ve decided to do to help my team manage this process successfully is to create a custom tool with built in “queues” that will display their work items as they need attention without relying on the developer to manually run different queries and discover items on their own.

And so that brings us to the TFS SDK. I’m going write a series of short posts detailing how to do some simple tasks that can be combined to do very powerful things. One of the simplest and most powerful things you can do with the TFS SDK is run WIQL queries to query work items using a SQL-like syntax.

The first step you need to do is simply to add the TFS SDK references to your project. These are the three I added to my project:


Once you’ve got that, you can build an execute a query with the following block of code:

var tpc = new TfsTeamProjectCollection(new Uri("http://YourTfsServer:8080/tfs/YourCollection"));
var workItemStore = tpc.GetService(); 
var queryResults = workItemStore.Query(@"
    SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State] 
    FROM WorkItems 
    WHERE [System.AssignedTo] = @me 
    ORDER BY [System.Id]
foreach (WorkItem wi in queryResults)
    Console.WriteLine("{0} | {1}", wi.Fields["State"].Value, wi.Fields["Title"].Value);

Bonus tip: you can construct your WIQL query by building it from Visual Studio’s Team Explorer and saving it to file. For my application, I’m storing the WIQL query in my app.config to allow for quick & easy customization.


Private Builds in TFS

My team has configured TFS to automatically kick-off continuous integration (CI) builds when developers check-in changes. The build depends on the affected area, and the time to complete depends on the build. Some of our builds contains hundreds of projects and thousands of unit tests, and these can take the better part of an hour to complete. We also have nightly builds that build even more and take even longer.

Occasionally, I’ll have some changes to check-in at the end of the day. I check them in, and I get a notification that I broke the build when I’m halfway home. Crap. I need to fix the change before the nightly build starts, or it’s going to break, too.

Wouldn’t it be great if there was a way to run a build with my pending changes and then check them in automatically if the build succeeds? Well… There is! TFS has support for private builds (or “buddy builds”) that you can queue with a shelveset.

When I choose to queue a build, I choose to build with local sources plus a shelveset. Then I specify which shelveset, and I have the option to check-in my changes if the build succeeds. Splendid!

%d bloggers like this: