TFS Build API by Example #2: Get the Latest Build.

Often when doing some build integration work with the TFS2008 Build API, you will want to get the latest build for a particular definition – for example to show the latest status of the build etc.  To do that use the following code snippet:

TeamFoundationServer tfs = new TeamFoundationServer("http://tfsserver:8080");
IBuildServer buildServer = (IBuildServer) tfs.GetService(typeof(IBuildServer));

IBuildDetailSpec buildDetailSpec = buildServer.CreateBuildDetailSpec("Team Project", "Build Definition Name");
buildDetailSpec.MaxBuildsPerDefinition = 1;
buildDetailSpec.QueryOrder = BuildQueryOrder.FinishTimeDescending;

IBuildQueryResult results = buildServer.QueryBuilds(buildDetailSpec);
if (results.Failures.Length == 0 && results.Builds.Length == 1)
{
    IBuildDetail buildDetail = results.Builds[0];
    Console.WriteLine("Build: " + buildDetail.BuildNumber);
    Console.WriteLine("Account requesting build “ +
      “(build service user for triggered builds): " + buildDetail.RequestedBy);
    Console.WriteLine("Build triggered by: " + buildDetail.RequestedFor);
}

 

Note that if you are wanting to display a continually updated list of builds, then you should take a look at the CreateQueuedBuildsView method on IBuildServer as a lot of the hard work is done for you.  See my Build Wallboard example on the MSDN Code Gallery for more information.

Archives

Creative Commons License
This blog is licensed under a Creative Commons License.