Saturday, January 09, 2010

Using the TFS 2010 SDK (Beta 2)

With significant changes to the way TFS 2010 works, especially in relation to Team Build and the new concept of Projects Collections, the API has necessarily adapted to account for these changes. For developers wanting to use these new API’s, there is currently very little in the way of documentation. The TFS SDK code gallery site has a small amount of examples, as well as the beginnings of the API documentation, which although sparse, because of reasonably clear naming conventions actually offers users some guidance when attempting to write integration software for TFS 2010. There are also a few other bloggers around who have some posted code examples.

I have been looking at re-writing TFS Dependency Replicator for TFS 2010, and in so doing have created a very simple application to enumerate some of the objects I am interested in.

In the interests of helping other developers, here is my source code. You can also download the full solution from here.

To run it, you must have .Net 4.0 beta 2 and TFS 2010 Explorer Beta 2 installed on your machine.

class Program
{
static void Main(string[] args)
{

// Get a list of all registered Project Collections
Console.WriteLine("Project Collections");
var collections = RegisteredInstances.GetProjectCollections();

foreach (var collection in collections)
{
Console.WriteLine("Connecting to TFS Server {0} at {1}", collection.Name, collection.Uri.ToString());

// connect to tfs
using (var tfs = new TeamFoundationServer(collection.Uri))
{
tfs.EnsureAuthenticated();
Console.WriteLine("Successfully connected to {0}", tfs.Uri.ToString());

// Get items of interest from TFS
EnumerateServer(tfs);
}
}

Console.ReadLine();
}

private static void EnumerateServer(TeamFoundationServer tfs)
{
// Get all of the Team Projects
var structureService = tfs.GetService();
var teamProjects = structureService.ListAllProjects();

Console.WriteLine("Team Projects");

foreach (var teamProject in teamProjects)
{
Console.WriteLine("\tName: {0}", teamProject.Name);
}

// extract all the work items
// NOTE: this uses wiql to query the work item store
Console.WriteLine("Work Items");
var workItemStore = tfs.GetService();
var workItems = workItemStore.Query(
"SELECT [ID], [Title] FROM WorkItems");

foreach (WorkItem workItem in workItems)
{
Console.WriteLine("\tID: {0}\tTitle: {1}", workItem.Id, workItem.Title);
}

// Get the build servers
Console.WriteLine("Build Servers");
var buildServer = tfs.GetService();
var buildControllers = buildServer.QueryBuildControllers(true);

// Get all the build controllerrs
foreach (var buildController in buildControllers)
{
Console.WriteLine("\tController: {0}", buildController.Name);

// Get all the build agents
foreach (var agent in buildController.Agents)
{
Console.WriteLine("\t\tAgent: {0}, Team Project: {1}", agent.Name, agent.TeamProject);
}
}

foreach (var teamProject in teamProjects)
{
// Get each build definition for this Team Project
var buildDefinitions = buildServer.QueryBuildDefinitions(teamProject.Name);

foreach (var buildDefinition in buildDefinitions)
{
Console.WriteLine("\tBuild: {0}", buildDefinition.Name);

// Get all build Details for this build definition
var buildDetails = buildDefinition.QueryBuilds();

foreach (var buildDetail in buildDetails)
{
Console.WriteLine("\t\tBuild Number: {0} Build Status: {1}", buildDetail.BuildNumber, buildDetail.Status);
}
}
}

}
}

1 comment:

  1. Nice writeup on how to hunt for the performance issues, thanks for sharing!!..

    ReplyDelete