20 August 2013 CI Team

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("[email protected]", "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.