Udostępnij za pośrednictwem


Code snippet: How to convert an account (DOMAINuser) to its friendly name?

One of the changes we made post-Beta3 that you may have heard about in the forums is that our Work Item Tracking will default to using friendly display names instead of accounts (for instance, "James Manning" instead of "NORTHAMERICA\jmanning").  Since most (if not all) of our code snippets and examples display the account names, I figured it would be good to post how to convert that to a friendly name for those that may want to modify the code to do so.  Thankfully, it's as simple as using our IGroupSecurityService to call ReadIdentity.  In terms of the user string to pass, when at all possible, use the fully qualified domain\user form.  If 1) the user is in the same domain you are or 2) you're using the workgroup mode, then it isn't necessary, but especially for setups using multiple active directory domains, it's good to be specific about the user you're querying.

You'll need to reference Microsoft.TeamFoundation.dll (for GSS) and Microsoft.TeamFoundation.Client.dll (for TeamFoundationServer{,Factory}), both of which should be in the client's GAC for machines with Team Explorer installed.

C:\>GetFriendlyName.exe jmanning-test northamerica\jmanning
User northamerica\jmanning resolved as James Manning <nospam@microsoft.com>

C:\>GetFriendlyName.exe jmanning-test bogus
Server jmanning-test could not resolve user bogus

 using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;

namespace GetFriendlyName
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 2 || args[0] == "/?" || args[0] == "-?")
            {
                Usage();
                Environment.Exit(1);
            }

            TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(args[0]);
            IGroupSecurityService gss = (IGroupSecurityService)tfs.GetService(typeof(IGroupSecurityService));

            Identity identity = gss.ReadIdentity(SearchFactor.AccountName, args[1], QueryMembership.None);
            if (identity == null)
            {
                Console.Error.WriteLine("Server {0} could not resolve user {1}", args[0], args[1]);
                Environment.Exit(1);
            }
            else
            {
                Console.WriteLine("User {0} resolved as {1} <{2}>", args[1], identity.DisplayName, identity.MailAddress);
            }
        }

        private static void Usage()
        {
            Console.Error.WriteLine("Copyright (c) Microsoft Corporation");
            Console.Error.WriteLine("");
            Console.Error.WriteLine("GetFriendlyName <server> <account>");
        }
    }
}

Comments

  • Anonymous
    August 01, 2006
    When&amp;nbsp;writing an application that pulls data from TFS (Team Foundation Server)&amp;nbsp;to&amp;nbsp;perform...
  • Anonymous
    August 12, 2006
    I recently had to put together a list of links to code samples.&amp;nbsp; This isn't even close to comprehensive,...
  • Anonymous
    August 12, 2006
    I recently had to put together a list of links to code samples.&amp;nbsp; This isn't even close to comprehensive,...