Saturday, April 17, 2010

TFS2010: Connecting using TFS 2008 object model

Can you connect to TFS 2010 server using 2008 client? Sure you can! There are couple of gotchas to be aware of, specifically you’d be better off installing an update for TFS 2008; if you do not the following message comes up:

Grant Holliday wrote very detailed post on what needs to be installed and what to do if for some reason you cannot install the update.

However, I wanted to mention this in context of custom tools that use object model. While connecting to TFS 2010 with Team Explorer 2008 will produce the message above (and will require extra steps such as update installation to get TE working), the message won’t appear for your custom tools. In fact, if the connection to TFS 2010 is defined in registry you will connect and may not know the difference from inside of your tool.

Thus if you have custom tools using 2008 OM connecting to TFS 2010, it is your responsibility that the update gets installed correctly on machines with the tools.

Mirror from my MSDN blog

Thursday, April 15, 2010

TFS2010: Who am I? (getting logged in user info)

When you login to TFS using object model, sometimes it may be useful to get information about logged in user (such as domain, account, email etc.).

In TFS 2008 object model, one would get the logged in user display name, domain name, account name and email as following:

    1 TeamFoundationServer server = new TeamFoundationServer("tfsserver",

    2        new System.Net.NetworkCredential("domain_name\\user_name", "pwd"));

    3 server.EnsureAuthenticated();

    4 

    5 Console.Write(server.AuthenticatedUserDisplayName);

    6 // returns user's display name

    7 

    8 Console.Write(server.AuthenticatedUserName);

    9 // returns domain_name\user_name

   10 

   11 // AuthenticatedUserIdentity is Microsoft.TeamFoundation.Server.Identity

   12 Console.Write(server.AuthenticatedUserIdentity.Domain + "\\" +

   13         server.AuthenticatedUserIdentity.AccountName);

   14 // returns domain_name\user_name  

   15        

   16 Console.Write(server.AuthenticatedUserIdentity.MailAddress); 

   17 // returns user’s email  

In TFS 2010 OM, AuthenticatedUserDisplayName, AuthenticatedUserName and AuthenticatedUserIdentity properties are all deprecated, and instead AuthorizedIdentity property is introduced on Team Project Collection class. The code below makes use of the new property to retrieve logged in user information:

    1 TfsTeamProjectCollection collection = new TfsTeamProjectCollection(

    2        new Uri(http://tfsserver:8080/tfs/defaultcollection,

    3        new System.Net.NetworkCredential("domain_name\\user_name", "pwd"));

    4 collection.EnsureAuthenticated();

    5 

    6 // AuthorizedIdentity is Microsoft.TeamFoundation.Framework.Client.TeamFoundationIdentity

    7 Console.Write(collection.AuthorizedIdentity.DisplayName);

    8 // returns user's display name

    9 

   10 Console.Write(collection.AuthorizedIdentity.GetAttribute("Domain", "default"));

   11 // returns domain_name

   12 

   13 Console.Write(collection.AuthorizedIdentity.GetAttribute("Account", "default"));

   14 // returns user_name

   15 

   16 Console.Write(collection.AuthorizedIdentity.GetAttribute("Mail", "default")); 

   17 // returns user’s email

Mirror from my MSDN blog

Tuesday, April 13, 2010

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. “http://tfsserver:8080/tfs/DefaultCollection”).

Mirror from my MSDN blog

Thursday, November 20, 2008

Case of duplicate VSMDI files – resolved?

If you are using VSMDI files, chances are you know what I am talking about. You get all your test lists defined, tests running, sun is shining and suddenly, out of nowhere second and then third VSMDI files get created.

The problem is pretty irritating one, as can be witnessed by some angry posts at MSDN. The problem also seems to be a complicated one because it can happen in multiple scenarios, and some of those scenarios are not readily reproducible. The reproducible scenarios are:

  • The solution with VSMDI file is not under version control, but VSMDI file is read-only. When Visual Studio needs to change it, new file (with suffix 1, 2 etc.) is created
  • The solution with VSMDI file is under version control, and VSMDI file is checked out with exclusive lock. When Visual Studio needs to change it, automatic check out is attempted and fails, and new file (with suffix 1, 2 etc.) is created

Other scenarios appear to involve Test List editor tool window, and not reproducible in 100% of cases.

So you’d say – that’s all very interesting, but how about fixing that? There are several ways to go about it.

First of all, scenarios involving read-only VSMDI files should be easily fixed (by removing r/o flag). In case of source controlled solution, there is KB support article 957358 detailing steps to avoid that.

Secondly, there is piece of advice that I really like (courtesy of Chris Menegay) – always modify VSMDI files by double-clicking the file in Solution Explorer (and not by using Test List editor etc.). From my experience, it appeared to work magically.

And lastly, most of these scenarios are fixed in Visual Studio 2008 SP1. If you have installed service pack, and still observe duplicated files appearing, folks at MS would be every interested to know about it. Leave a comment or drop me a line.

In next version of Visual Studio, VS 2010, the pain associated with VSMDI files should go away as there is a whole lot of changes in the way testing is done.

Tuesday, August 19, 2008

Static Analysis for T-SQL

One little known feature of Visual Studio Team Edition for Database Professionals (aka Data Dude) is the ability to run static analysis on SQL scripts (similar to static code analysis for managed code). To be more precise, the analysis feature is part of Power Tools for Data Dude (and part of VSTS 2008 Database Edition GDR CTP).

I was going to look into that feature for quite a while; but sadly the feature’s lacking documentation and I did not have time to dig so I was postponing it. Today I come across very interesting review of the analysis feature, that provides missing documentation and more. Check out “Analyzing T-SQL Static Analysis 2005 & 2008” article at Mike Fourie’s blog.

And while there, you might want to have a look at Mike’s blog; it is one of the few blogs that deals both with MSBuild and TFS topics (for those who don’t know, Mike is the main person behind SDC tasks project at CodePlex).