Monday, July 19, 2010

Retrieve a list with all your TFS Users

The following code demonstrates how to read all users from different TFS collections.

namespace Migrator
{

using System;
using System.Linq;
using System.Reflection;
using Microsoft.TeamFoundation.Client;
using System.Net;
using Microsoft.TeamFoundation.Server;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Framework.Client;
using System.Collections.ObjectModel;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.VersionControl.Client;


public static class Helper
{
// without collection name
public static string url = @"http://tfs.yoururl.com/tfs";

[System.Diagnostics.DebuggerStepThrough]
public static TfsConfigurationServer GetTfsConfigurationServer()
{
return new TfsConfigurationServer(new Uri(url), GetCredentials(), new UICredentialsProvider());
}

[System.Diagnostics.DebuggerStepThrough]
private static ICredentials GetCredentials()
{
return new NetworkCredential("adminuser", "password", "YourDomain");
}

[System.Diagnostics.DebuggerStepThrough]
public static bool EnsureAuthentication(TfsConfigurationServer srv)
{
bool result = true;

try
{
srv.EnsureAuthenticated();
srv.Authenticate();
result = srv.HasAuthenticated;
}
catch (Exception)
{
result = false;
}

return result;
}

public static List<Identity> GetAllTfsUsers()
{
List<Identity> result = new List<Identity>();
TfsConfigurationServer srv = Helper.GetTfsConfigurationServer();

if (EnsureAuthentication(srv))
{
CatalogNode configurationServerNode = srv.CatalogNode;

// Query the children of the configuration server node for all of the team project collection nodes
ReadOnlyCollection<CatalogNode> tpcNodes = configurationServerNode.QueryChildren(
new Guid[] { CatalogResourceTypes.ProjectCollection },
false,
CatalogQueryOptions.None
);

foreach (CatalogNode tpcNode in tpcNodes)
{
Guid tpcId = new Guid(tpcNode.Resource.Properties["InstanceId"]);
TfsTeamProjectCollection tpc = srv.GetTeamProjectCollection(tpcId);

Console.WriteLine("{0}", tpc.Name);

// get a reference to the work item tracking service
var workItemStore = tpc.GetService<WorkItemStore>();

// go over the next node if no projects available
if (workItemStore.Projects.Count <= 0)
{
continue;
}

// iterate over the projects
foreach (Project project in workItemStore.Projects)
{
Console.WriteLine("\tProject: {0}", project.Name);
try
{
VersionControlServer versionControl = (VersionControlServer)tpc.GetService(typeof(VersionControlServer));
TeamProject teamProject = versionControl.GetTeamProject(project.Name);
IGroupSecurityService gss = (IGroupSecurityService)tpc.GetService<IGroupSecurityService>();
Identity[] appGroups = gss.ListApplicationGroups(teamProject.ArtifactUri.AbsoluteUri);

foreach (Identity group in appGroups)
{
Identity[] groupMembers = gss.ReadIdentities(SearchFactor.Sid, new string[] { group.Sid }, QueryMembership.Expanded);
foreach (Identity member in groupMembers)
{
if (member.Members != null)
{
foreach (string memberSid in member.Members)
{
Identity memberInfo = gss.ReadIdentity(SearchFactor.Sid, memberSid, QueryMembership.None);
if (memberInfo.Type == IdentityType.WindowsUser)
{
if (!result.Contains(memberInfo))
{
result.Add(memberInfo);
Console.WriteLine("\t\t" + memberInfo.AccountName + " - " + memberInfo.DisplayName + " - " + memberInfo.Domain);
}
else
{
Console.WriteLine("\t\tUser already available " + memberInfo.AccountName);
}

}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("\tThe Project: '{0}' throws an exception: {1} and will be ignored.", project.Name, ex.Message);
}
} // foreach (Project project in workItemStore.Projects)
} // foreach (CatalogNode tpcNode in tpcNodes)

}
else
{
Console.WriteLine("Authentication problem!");
}

return result;
}



}
}

No comments:

Shared Cache - .Net Caching made easy

All information about Shared Cache is available here: http://www.sharedcache.com/. Its free and easy to use, we provide all sources at codeplex.

Facebook Badge