Kristian Kristensen’s Blog


Connecting to the build server in TFS 2010 RC

Posted in Code,Microsoft,TFS by Kristian Kristensen on the February 19th, 2010

As part of my current project we’ve upgraded from Microsoft Team Foundation Server (TFS) 2008 to TFS 2010. Part of the project gungho is to illustrate the status of the current build. Hence we need some sort of programmatic way of accessing the build server history of TFS. This part has obviously changed  from 2008 to 2010, but also from 2010 Beta 2 to the Release Candidate. So I thought I’d detail how you can connect to the build portion of TFS 2010 RC.

First you want to add references to the new TFS SDK dll’s. They’re located in “C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0″. You’ll need:

  • Microsoft.TeamFoundation.Build.Client
  • Microsoft.TeamFoundation.Build.Common
  • Microsoft.TeamFoundation.Common
  • Microsoft.TeamFoundation.Client

We’ll need the following namespaces included:

using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client; 

Then setup the connection to your TFS server and make sure we authenticate:

Uri tfsUri = new Uri("http://tfsserver:8080/tfs/");
var tfssvr = new TfsConfigurationServer(tfsUri, new UICredentialsProvider());
tfssvr.EnsureAuthenticated();

A new feature in TFS 2010 is project collections, that group team projects. Therefore we need to locate the project collection where our team project is located. I know that our collection is called “Default” so I just look for that.

TeamProjectCollection tpc = null;
var collections = tfssvr.GetService<ITeamProjectCollectionService>().GetCollections();
foreach (var teamProjectCollection in collections)
{
    if (teamProjectCollection.Name.Equals("Default"))
    {
        tpc = teamProjectCollection;
    }
}

Next we retrieve a reference to the build server service running inside our project collection. Using this we create reference to the build queue for our specific team project. We can filter this build queue so we only get information about builds in progress and builds that are succeeded. We can also hook up an event handler so we’ll be called whenever there are changes to the build queue; we’ll look at this event handler in a minute. Finally we connect to the build queue, thereby setting up our callback.

IBuildServer buildServer = tfssvr.GetTeamProjectCollection(tpc.Id).GetService<IBuildServer>();

var queue = buildServer.CreateQueuedBuildsView("MyProject");
queue.StatusFilter = QueueStatus.InProgress | QueueStatus.Completed;

queue.StatusChanged += new StatusChangedEventHandler(queue_StatusChanged);

queue.Connect();

The event handler that’ll get called whenever there’s a change in the build queue is located below. We cast the sender to the right interface so we can access the queue. We verify that there’s indeed a change in the build queue, and that we’re not just being pinged. We extract the latest build (the list is sorted by TFS), and then we simply print out some information about the build.

static void queue_StatusChanged(object sender, StatusChangedEventArgs e)
{
  var queue = (IQueuedBuildsView) sender;
  if (e.Changed && queue.QueuedBuilds.Length > 0)
  {
    var curBuild = queue.QueuedBuilds[queue.QueuedBuilds.Length - 1];
    Console.WriteLine("New build detected: " + curBuild.Build.BuildNumber + " is " + curBuild.Build.Status.ToString());
}

The interesting use case for this is of course to hook in your own customer build notification system in the event handler. In my current project we have a Nabaztag bunny, that’ll inform the team when a new build is in progress and when it’s finished. You can see a video of it on Youtube – IronBunny.

Ewald Hofman pointed me in the right direction with his posts on how the SDK works in TFS 2010 Beta 2.

Disclaimer: This code is just an example, and you really shouldn’t put it blindly into production. Basically the usual “Works on my machine”.

  • If you like my writing you should subscribe to my RSS feed.

    Leave a Reply

    You must be logged in to post a comment.