Sample Code: Connect to Team Foundation Server

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.


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.












































Comments (9)
  1. Mohan Singh says:

    I am trying the UI coded test to connect to test case data source but getting the error

    The constructor to deserialize an object of type 'Microsoft.TeamFoundation.Framework.Client.AccessCheckException' was not found.

  2. Mohan, does this help? How to: Create a Data-Driven Coded UI Test (…/ee624082.aspx)

  3. Hi Allen,

    Thank you for the article. I have two questions;

    1. Can we pragmatically access the results returned by the queries saved in TFS?
    2. Can we modify any TFS item pragmatically, like change the item state or adding comments in the history, etc.

    Thank you for your comments on these questions.



  4. Yes, you can do that. Look here to get started:…/bb130146.aspx

  5. Quandary says:

    Actually, you find them at:

    C:Program Files (x86)Microsoft Visual Studio 11.0Common7IDEReferenceAssembliesv2.0

  6. Mohamed Sameel says:

    Hi Allen,

    I've a question may be that is easy for you. How can I check TFS TeamProjectCollection status programmatically? when I try to get all the team project details from a collection it throws an exception because its stopped working (Not online or  Status = stopped)



  7. @Mohamed, see if this works:

    IEnumerable<Guid> startedProjectCollectionIds = configurationServer.GetService<ITeamProjectCollectionService>().GetCollections()

                   .Where(collection => collection.State == TeamFoundationServiceHostStatus.Started)

                   .Select(collection => collection.Id);

  8. Prateek says:

    Hi Allen

    I want to send custom email notification and run rule set on code review submit event. How can i achieve this ?

    Thank you for your help

Comments are closed.

Skip to main content