Connecting to Team Foundation Server programmatically isn't as obvious as you might expect. Here's a topic that describes how to do that that. It'll be included in the TFS SDK when we publish that. Let me know whether this info is helpful, and what else you'd like to see.

My next post will cover doing some things with work item tracking. Hint: the service to get is WorkItemStore.

Allen

You can programmatically connect to Team Foundation and then access the team projects in each of the team project collections on that server. You can list the team project collections and the team projects that they contain using the following example. To use this example, create a C# console application, add references to Microsoft.TeamFoundation.Client and Microsoft.TeamFoundation.Common, replace the contents of Program.cs with the example, and then run the program or step through it in the debugger.

Note   If Microsoft.TeamFoundation.Client and Microsoft.TeamFoundation.Common do not appear in the .NET tab of the References dialog, then use the Browse tab to add the assemblies. You can find them at Program Files\Microsoft Visual Studio 10.0\Common7\IDE\v2.0.

using System;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.Text;

using Microsoft.TeamFoundation.Client;

using Microsoft.TeamFoundation.Framework.Client;

using Microsoft.TeamFoundation.Framework.Common;

 

namespace TfsApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            // Connect to Team Foundation Server

            //     server is the name of the server that is running the Team Foundation application-tier.

            //     port is the port that Team Foundation uses. The default is port is 8080.

            //     vpath is the virutal path to the Team Foundation application. The default path is tfs.

            TfsConfigurationServer configurationServer =

                TfsConfigurationServerFactory.GetConfigurationServer(new Uri("http://server:port/vpath"));

 

            // Get the catalog of team project collections

            CatalogNode catalogNode = configurationServer.CatalogNode;

            ReadOnlyCollection<CatalogNode> tpcNodes = catalogNode.QueryChildren(

                new Guid[] { CatalogResourceTypes.ProjectCollection },

                false, CatalogQueryOptions.None);

 

            // List the team project collections

            foreach (CatalogNode tpcNode in tpcNodes)

            {

                // Use the InstanceId property to get the team project collection

                Guid tpcId = new Guid(tpcNode.Resource.Properties["InstanceId"]);

                TfsTeamProjectCollection tpc = configurationServer.GetTeamProjectCollection(tpcId);

 

                // Print the name of the team project collection

                Console.WriteLine("Collection: " + tpc.Name);

 

                // Get catalog of team projects for the collection

                ReadOnlyCollection<CatalogNode> tpNodes = tpcNode.QueryChildren(

                    new Guid[] { CatalogResourceTypes.TeamProject },

                    false, CatalogQueryOptions.None);

 

                // List the team projects in the collection

                foreach (CatalogNode tpNode in tpNodes)

                {

                    Console.WriteLine(" Team Project: " + tpNode.Resource.DisplayName);

                }

            }

        }

    }

}

You can access additional services using the GetService method of the TfsConfigurationServer or TfsTeamProjectCollection classes. When you use the TfsConfigurationServer class, you are accessing the services for the entire server. When you use the TfsTeamProjectCollection class, you are accessing the services for the team project collection. For example, the ITeamFoundationRegistry service for TfsConfigurationServer provides registered properties of the server, and the same service for TfsTeamProjectCollection provides the registered properties of a team project collection.

 

Service

TfsConfigurationServer

TfsTeamProjectCollection

Microsoft.TeamFoundation.Framework.Client.ITeamFoundationRegistry

Microsoft.TeamFoundation.Framework.Client.IIdentityManagementService

Microsoft.TeamFoundation.Framework.Client.ITeamFoundationJobService

Microsoft.TeamFoundation.Framework.Client.IPropertyService

Microsoft.TeamFoundation.Framework.Client.IEventService

Microsoft.TeamFoundation.Framework.Client.ISecurityService

Microsoft.TeamFoundation.Framework.Client.ILocationService

Microsoft.TeamFoundation.Client.TswaClientHyperlinkService

Microsoft.TeamFoundation.Framework.Client.ITeamProjectCollectionService

Microsoft.TeamFoundation.Framework.Client.IAdministrationService

Microsoft.TeamFoundation.Framework.Client.ICatalogService

Microsoft.TeamFoundation.Lab.Client.LabFrameworkService

Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer

 

Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore

 

Microsoft.TeamFoundation.Build.Client.IBuildServer

 

Microsoft.TeamFoundation.TestManagement.Client.ITestManagementService

 

Microsoft.TeamFoundation.Lab.Client.LabFrameworkService

 

Microsoft.TeamFoundation.Lab.Client.LabAdminService

 

Microsoft.TeamFoundation.Lab.Client.LabService

 

Microsoft.TeamFoundation.Lab.WorkflowIntegration.Client.IWorkflowIntegrationService

 

Microsoft.TeamFoundation.Lab.TestIntegration.Client.ITestIntegrationService

 

Microsoft.TeamFoundation.ILinking

 

Microsoft.TeamFoundation.Server.ICommonStructureService3

 

Microsoft.TeamFoundation.Server.IServerStatus

 

Microsoft.TeamFoundation.Server.IProcessTemplates