TFS2010: Getting connected in new object model

When starting up with TFS 2010 object model, there are few gotchas to be aware of (especially if you did coding for TFS OM 2005/2008 in the past). See my previous post for high-level details:

In 2008 one would establish connection to TFS as follows

   1 string[] servers = RegisteredServers.GetServerNames();

   2 

   3 // select one of the servers (in real life combo box?)

   4 string serverName = servers[0];

   5 

   6 TeamFoundationServer server = new TeamFoundationServer(serverName,

   7         new System.Net.NetworkCredential(userName, password));

   8 

   9 server.EnsureAuthenticated();

In Team Foundation Server 2010 the code becomes:

   1 RegisteredTfsCollection[] projects = RegisteredTfsConnections.GetProjectCollections();

   2 List<string> servers = new List<string>();

   3 foreach (RegisteredTfsCollection collection in collections)

   4 {

   5    if (!collection.Offline)

   6      results.Add(collection.Uri.ToString());

   7 }

   8 

   9 // select one of the servers (in real life combo box?)

  10 string serverName = servers[0];

  11

  12 TfsTeamProjectCollection server = new TfsTeamProjectCollection(new Uri(tfsServerName),

  13                 new System.Net.NetworkCredential(userName, password));

  14 

  15 server.EnsureAuthenticated();

Should be no big surprises considering the changes in 2010 – one server w\projects became multiple projects collections. One thing to be aware of is the change in the arguments you pass to constructor of TfsTeamProjectCollection; in 2005/2008 one was able to pass server name (i.e. “tfserver”) as retrieved by using RegisteredServers class. In 2010 that becomes impossible since the server may contain multiple collections; thus you have to pass full URI (e.g. “https://tfsserver:8080/tfs/DefaultCollection”).