I recently had a request to list all project collections and the projects that are in the collections Here is what I came up with to do the trick. Maybe it will help someone else. I used the following website to help me out
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using System.Collections.ObjectModel;
using System.Net;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
namespace TFS2010ProjectList
{
class Program
{
// without collection name
public static string url = @”http://tfsserverurl:8080/tfs”;
static void Main(string[] args)
{
GetProjects();
}
private static void GetProjects()
{
TfsConfigurationServer srv = 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 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);
var workItemStore = tpc.GetService();
// 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);
}
}
}
}
[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("username", "password", "Domain");
}
[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;
}
}
}
[...] List all TFS Project Collections and Projects [...]
Pingback by Links – 11/09/2010 « Team System Rocks! — November 9, 2010 @ 2:09 pm