5

How to get the collections from TFS using TFS API

Please refer here for more details. This is one of the best resources on TFS stuff.

Martin
  • 3,396
  • 5
  • 41
  • 67

1 Answers1

8
private TfsConfigurationServer configurationServer;
configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(uri);



public IList<KeyValuePair<Guid, String>> GetCollections()
{
    //ApplicationLogger.Log("Entered into GetCollections() : ");
    var collectionList = new List<KeyValuePair<Guid, String>>();
    try
    {
        configurationServer.Authenticate();

        ReadOnlyCollection<CatalogNode> collectionNodes = configurationServer.CatalogNode.QueryChildren(
            new[] { CatalogResourceTypes.ProjectCollection },
            false,
            CatalogQueryOptions.None);
        foreach (CatalogNode collectionNode in collectionNodes)
        {
            var collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
            TfsTeamProjectCollection teamProjectCollection =
                configurationServer.GetTeamProjectCollection(collectionId);

            if (teamProjectCollection == null)
                continue;

            collectionList.Add(new KeyValuePair<Guid, String>(collectionId, teamProjectCollection.Name));
        }
    }
    catch (Exception e)
    {
        ApplicationLogger.Log(e);
    }

    return collectionList;
}

Each returned key value pair in the list contains the collection guid and collections name

Martin
  • 3,396
  • 5
  • 41
  • 67
  • 1
    Try not to put error handling code in to examples. It's not relevant and it changes the behaviour. In this case if the TFS server does not response, your method declares that there are simply no Collections. – Uatec Jan 17 '14 at 08:53