Connecting to TFS2010 with API

If you know TFS 2008 API and try to connect to a tfs 2010 you could probably start with this snippet, but you will get a 404 exception, telling you that the tfs is not available.

1
2
3
TeamFoundationServer tfs = new TeamFoundationServer("http://vs2010beta2:8080/tfs",
new NetworkCredential("dorikr", "P2ssw0rd"));
tfs.EnsureAuthenticated();

The problem is originated by the new architecture of tfs2010, and the presence of Project collections. Project collections are containers for Team Project, and they are the analogue of a TFS2008 server. The above code cannot work, because you are trying to connect with a TeamFoundationServer object to a tfs that contains a certain number of project collection. To make it work you need to change the server string passed to the constructor in this way

1
2
3
TeamFoundationServer tfs = new TeamFoundationServer("http://vs2010beta2:8080/tfs/DefaultCollection",
new NetworkCredential("dorikr", "P2ssw0rd"));
tfs.EnsureAuthenticated();

This works perfectly, because now you can use the TeamFoundationServer object to do whatever you want. If you are curious and you want to enumerate project collections avaliable in a server you can use this piece of code:

1
2
3
4
5
Uri configurationServerUri = new Uri("http://vs2010beta2:8080/tfs");
TfsConfigurationServer configurationServer =
new TfsConfigurationServer(configurationServerUri, new NetworkCredential("dorikr", "P2ssw0rd"));
ITeamProjectCollectionService tpcService = configurationServer.GetService<ITeamProjectCollectionService>();
var collections = tpcService.GetCollections();

As you can see the TfsConfigurationServer object can be queried with the familiar GetService<T> method asking for a ITeamProjectCollectionService, that has the method GetCollections() to have a list of all Team Project Collection present on the server.

image

alk.

Tags: Team Foundation Server