Tuesday, December 22, 2009

Get User Permissions on TFS team project

This code is based on the listing in http://social.msdn.microsoft.com/Forums/en/tfsadmin/thread/9f977d37-23d8-4b07-a5dd-6cdc6d9dc6a6

Requires 2 parameters: windows user id, and team project name
Returns a List of groups in the team project that the user has permissions for


public static List <string> GetAccountPermissions(string account, string projectName)
{
    List <string> groupList = new List <string>();
    String serverUri = @"http://serverName:8080/";
    // connect to the server
    TeamFoundationServer server = new TeamFoundationServer(serverUri);
    // get ICommonStructureService (later to be used to list all team projects)
    ICommonStructureService iss = (ICommonStructureService)server.GetService(typeof(ICommonStructureService));
    // get IGroupSecurityService (later to be used to retrieve identity information)
    IGroupSecurityService2 gss = (IGroupSecurityService2)server.GetService(typeof(IGroupSecurityService2));
    ProjectInfo[] allTeamProjects = iss.ListProjects();
    // iterate thru the team project list
    foreach (ProjectInfo pi in allTeamProjects)
    {
    // ProjectInfo gives you the team project's name, status, and URI.
        String teamProjectName = pi.Name;
        if (pi.Name == projectName)
        {
            Identity[] allProjectGroups = gss.ListApplicationGroups(pi.Uri);
            // iterate thru the project group list and get a list of direct members for each project group
            foreach (Identity projectGroup in allProjectGroups)
            {
                Identity pg = gss.ReadIdentity(SearchFactor.Sid, projectGroup.Sid, QueryMembership.Direct);
                foreach (String groupMemberSid in pg.Members)
                {
                    Identity m = gss.ReadIdentity(SearchFactor.Sid, groupMemberSid, QueryMembership.None);
                    if (m.AccountName == account)
                    {
                        groupList.Add(pg.DisplayName);
                    }
                }
            }
        }
    }
    return groupList;
}

Thursday, December 17, 2009

Check String For Null

It's easy to forget but C# has a handy method to check if a string is null or empty:

If(!String.IsNullOrEmpty(str))
...do something

Wednesday, December 9, 2009

Get List of Allowed Values in TFS Work Item Field

This is how you can obtain programmatically a list of allowed values in a TFS Work Item field (in the example below, in Priority field in Bug template):

TeamFoundationServer tfs = new TeamFoundationServer("myTFServer");

WorkItemStore wis = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));

Project teamProject = wis.Projects["myTeamProjectName"];

WorkItemType wit = teamProject.WorkItemTypes["Bug"];

 

FieldDefinition fd = wit.FieldDefinitions["Microsoft.VSTS.Common.Priority"];

AllowedValuesCollection avc = fd.AllowedValues;

 

foreach (var row in avc)

{

Console.WriteLine(row);

}