TFS API: Query Build-Definitions

The Team Foundation Server offers On-Premise and as „Cloud-TFS“ (Team Foundation Server) (German-Blogpost) several services (Build, WorkItems, Source Control,…). Those services are easy to use with the help of .NET APIs.

In this blogpost I try to call the last build results of a team-project.

Required assemblies

To request a build you won’t need all five assemblies but still you might need some of them sooner or later. You will find them in the visual studio installation directive (C:/Program Files (x86)/Microsoft Visual Studio 11.0/Common7/IDE/ReferenceAssemblies/v2.0-for VS 2012). You can copy them and integrate them in your own solution.

Here is the list of assemblies:

- Microsoft.TeamFoundation.Client.dll
- Microsoft.TeamFoundation.Common.dll
- Microsoft.TeamFoundation.VersionControl.Client.dll
- Microsoft.TeamFoundation.Build.Common.dll
- Microsoft.TeamFoundation.Build.Client.dll

Attention: You need to activate “Enable 32bit applications” in the IIS AppPool (if you want to use it with a WebApp). It seems like the assemblies are at least partly 32bit assemblies because it just wouldn’t work without this adjustment.

Demo Code:

Over the API I access the Team Foundation Service

class Program { static void Main(string[] args) { // Auth with UserName & Password (Microsoft Acc): //BasicAuthCredential basicCred = new BasicAuthCredential(new NetworkCredential("xxx@hotmail.com", "pw")); //TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred); //tfsCred.AllowInteractive = false; // //TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("https://code-inside.visualstudio.com/DefaultCollection"), tfsCred); TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("https://code-inside.visualstudio.com/DefaultCollection")); IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer)); var builds = buildServer.QueryBuilds("DrinkHub"); foreach (IBuildDetail build in builds) { var result = string.Format("Build {0}/{3} {4} - current status {1} - as of {2}", build.BuildDefinition.Name, build.Status.ToString(), build.FinishTime, build.LabelName, Environment.NewLine); System.Console.WriteLine(result); } // Detailed via http://www.incyclesoftware.com/2012/09/fastest-way-to-get-list-of-builds-using-ibuildserver-querybuilds-2/ var buildSpec = buildServer.CreateBuildDetailSpec("DrinkHub", "Main.Continuous"); buildSpec.InformationTypes = null; var buildDetails = buildServer.QueryBuilds(buildSpec).Builds; Console.WriteLine(buildDetails.First().Status); Console.ReadLine(); } }

image

How does the authentication work?

Basically the API runs all the time in the credentials of the users – if you access the team foundation service the Microsoft account is chosen by default. OnPremise the Windows account will be activated.

If the currently logged user doesn’t match an authentication windows will be opened. If the code runs on a server this might be a problem so it is better to directly choose a user (like you can see on the code above) – this works for Microsoft and Windows accounts.

Result

I was positively surprised about how easy you can access the TFS information’s – let’s see what else you can do with the API. The whole project is available on GitHub.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by

Learn more about our team.

One Response

  1. does the checkbox: enable 32 applications because those dlls are 2.0 framework? If so, do they have counterparts in 4.0 framework?

    Reply

Comment on this post

Recent Posts

  • How to access an Azure Website with the local IIS Manager

      Since the end of February it is possible to access an Azure Website with the IIS Manager. Although the Azure Management site offers some information’s there are more details visible at the IIS Manager.   For the connection you will need an IIS Manager and the IIS Manager for Remote Administration Extension. It’s also […]

  • image1929-570x143_thumb.png
    Create and validate own Json-Web-Tokens (JWTs)

    If you are interested in web authentication you probably have heard about JSON Web tokens (JWT). What is a JWT? Maybe I’m not using the correct security termination but however: JWTs are used to exchange claims between two systems. For example: You want to log on to a service (like Facebook, Twitter, etc.) and want […]

  • Micro-Optimization: how to shrink or „embed“ pictures

      I’m currently working on the “CodeInside Dashboard” and since the page structure isn’t that difficult it should be possible to fulfill all of Google Pagespeed or Yahoos YSlow recommendations. One of the rules was to optimize the 4 PNGs that are embedded on the page.   Before – without optimization: Below you can see […]

  • image1979-570x194.png
    Move to Windows Azure – VMs, Word Press Migration, DNS changes

    Since mid January this blogs runs on a WordPress installation in an Azure VM. Because I always thought that the subject is quite complicated this blogpost offers a view behind the scenes. Why this move? So far this blog (both German and English Version) runs on a hoster somewhere in Germany. The main problem with this […]

  • Windows Azure Active Directory – CRUD for users and groups

      Windows Azure Active Directory? If you are not informed about the subject I recommend you to have a look on this Azure Info site. Which resources are there? The Azure AD contains the following entities: - Users - Groups - Contacts - Roles Access to the directory or on the “directory graph” Although the […]

Support us