Partager via


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.

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("https://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

 

Comments

  • Anonymous
    February 26, 2012
    I am trying the UI coded test to connect to test case data source but getting the errorThe constructor to deserialize an object of type 'Microsoft.TeamFoundation.Framework.Client.AccessCheckException' was not found.

  • Anonymous
    February 27, 2012
    Mohan, does this help? How to: Create a Data-Driven Coded UI Test (msdn.microsoft.com/.../ee624082.aspx)

  • Anonymous
    February 24, 2013
    Hi Allen,Thank you for the article. I have two questions;Can we pragmatically access the results returned by the queries saved in TFS?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.Regards,Salman

  • Anonymous
    February 26, 2013
    Yes, you can do that. Look here to get started: msdn.microsoft.com/.../bb130146.aspx

  • Anonymous
    October 22, 2013
    Actually, you find them at: C:Program Files (x86)Microsoft Visual Studio 11.0Common7IDEReferenceAssembliesv2.0

  • Anonymous
    July 28, 2015
    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) Thanks Sameel

  • Anonymous
    August 03, 2015
    @Mohamed, see if this works: IEnumerable<Guid> startedProjectCollectionIds = configurationServer.GetService<ITeamProjectCollectionService>().GetCollections()                .Where(collection => collection.State == TeamFoundationServiceHostStatus.Started)                .Select(collection => collection.Id);

  • Anonymous
    February 21, 2016
    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

  • Anonymous
    February 22, 2016
    @Parteek, see if this is what you need: visualstudio.com/.../create-subscription Allen