TFS 2010 SDK: Connecting to TFS 2010 Programmatically–Part 1

Posted by Tarun Arora on Geeks with Blogs See other posts from Geeks with Blogs or by Tarun Arora
Published on Sat, 18 Jun 2011 15:05:00 GMT Indexed on 2011/06/20 16:24 UTC
Read the original article Hit count: 1952

Filed under:

 

Download Working Demo

Great! You have reached that point where you would like to extend TFS 2010. The first step is to connect to TFS programmatically.

1. Download TFS 2010 SDK => http://visualstudiogallery.msdn.microsoft.com/25622469-19d8-4959-8e5c-4025d1c9183d?SRC=VSIDE

image

2. Alternatively you can also download this from the visual studio extension manager

image

3. Create a new Windows Forms Application project and add reference to TFS Common and client dlls

Note - If Microsoft.TeamFoundation.Client and Microsoft.TeamFoundation.Common do not appear on the .NET tab of the References dialog box, use the Browse tab to add the assemblies. You can find them at %ProgramFiles%\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0.

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;

 

image

4. There are several ways to connect to TFS, the two classes of interest are,

Option 1 – Class – TfsTeamProjectCollectionClass
namespace Microsoft.TeamFoundation.Client
{
    public class TfsTeamProjectCollection : TfsConnection
    {
        public TfsTeamProjectCollection(RegisteredProjectCollection projectCollection);
        public TfsTeamProjectCollection(Uri uri);
        public TfsTeamProjectCollection(RegisteredProjectCollection projectCollection, IdentityDescriptor identityToImpersonate);
        public TfsTeamProjectCollection(Uri uri, ICredentials credentials);
        public TfsTeamProjectCollection(Uri uri, ICredentialsProvider credentialsProvider);
        public TfsTeamProjectCollection(Uri uri, IdentityDescriptor identityToImpersonate);
        public TfsTeamProjectCollection(RegisteredProjectCollection projectCollection, ICredentials credentials, ICredentialsProvider credentialsProvider);
        public TfsTeamProjectCollection(Uri uri, ICredentials credentials, ICredentialsProvider credentialsProvider);
        public TfsTeamProjectCollection(RegisteredProjectCollection projectCollection, ICredentials credentials, ICredentialsProvider credentialsProvider, IdentityDescriptor identityToImpersonate);
        public TfsTeamProjectCollection(Uri uri, ICredentials credentials, ICredentialsProvider credentialsProvider, IdentityDescriptor identityToImpersonate);

        public override CatalogNode CatalogNode { get; }
        public TfsConfigurationServer ConfigurationServer { get; internal set; }
        public override string Name { get; }

        public static Uri GetFullyQualifiedUriForName(string name);
        protected override object GetServiceInstance(Type serviceType, object serviceInstance);
        protected override object InitializeTeamFoundationObject(string fullName, object instance);
    }
}
Option 2 – Class – TfsConfigurationServer

namespace Microsoft.TeamFoundation.Client
{
    public class TfsConfigurationServer : TfsConnection
    {
        public TfsConfigurationServer(RegisteredConfigurationServer application);
        public TfsConfigurationServer(Uri uri);
        public TfsConfigurationServer(RegisteredConfigurationServer application, IdentityDescriptor identityToImpersonate);
        public TfsConfigurationServer(Uri uri, ICredentials credentials);
        public TfsConfigurationServer(Uri uri, ICredentialsProvider credentialsProvider);
        public TfsConfigurationServer(Uri uri, IdentityDescriptor identityToImpersonate);
        public TfsConfigurationServer(RegisteredConfigurationServer application, ICredentials credentials, ICredentialsProvider credentialsProvider);
        public TfsConfigurationServer(Uri uri, ICredentials credentials, ICredentialsProvider credentialsProvider);
        public TfsConfigurationServer(RegisteredConfigurationServer application, ICredentials credentials, ICredentialsProvider credentialsProvider, IdentityDescriptor identityToImpersonate);
        public TfsConfigurationServer(Uri uri, ICredentials credentials, ICredentialsProvider credentialsProvider, IdentityDescriptor identityToImpersonate);

        public override CatalogNode CatalogNode { get; }
        public override string Name { get; }

        protected override object GetServiceInstance(Type serviceType, object serviceInstance);
        public TfsTeamProjectCollection GetTeamProjectCollection(Guid collectionId);
        protected override object InitializeTeamFoundationObject(string fullName, object instance);
    }
}

 

Note – The TeamFoundationServer class is obsolete. Use the TfsTeamProjectCollection or TfsConfigurationServer classes to talk to a 2010 Team Foundation Server. In order to talk to a 2005 or 2008 Team Foundation Server use the TfsTeamProjectCollection class.

5. Sample code for programmatically connecting to TFS 2010 using the TFS 2010 API

How do i know what the URI of my TFS server is,

image

image

Note – You need to be have Team Project Collection view details permission in order to connect, expect to receive an authorization failure message if you do not have sufficient permissions.

Case 1: Connect by Uri

string _myUri = @"https://tfs.codeplex.com:443/tfs/tfs30";

TfsConfigurationServer configurationServer =
                TfsConfigurationServerFactory.GetConfigurationServer(new Uri(_myUri));
Case 2: Connect by Uri, prompt for credentials

string _myUri = @"https://tfs.codeplex.com:443/tfs/tfs30";

TfsConfigurationServer configurationServer =
    TfsConfigurationServerFactory.GetConfigurationServer(new Uri(_myUri), new UICredentialsProvider());
configurationServer.EnsureAuthenticated();
Case 3: Connect by Uri, custom credentials

In order to use this method of connectivity you need to implement the interface ICredentailsProvider

    public class ConnectByImplementingCredentialsProvider : ICredentialsProvider
    {
        public ICredentials GetCredentials(Uri uri, ICredentials iCredentials)
        {
            return new NetworkCredential("UserName", "Password", "Domain");
        }

        public void NotifyCredentialsAuthenticated(Uri uri)
        {
            throw new ApplicationException("Unable to authenticate");
        }
    }

And now consume the implementation of the interface,

    string _myUri = @"https://tfs.codeplex.com:443/tfs/tfs30";

    ConnectByImplementingCredentialsProvider connect = new ConnectByImplementingCredentialsProvider();
    ICredentials iCred = new NetworkCredential("UserName", "Password", "Domain");
    connect.GetCredentials(new Uri(_myUri), iCred);
            
    TfsConfigurationServer configurationServer =
                       TfsConfigurationServerFactory.GetConfigurationServer(new Uri(_myUri), connect);
    configurationServer.EnsureAuthenticated();

 

6. Programmatically query TFS 2010 using the TFS SDK for all Team Project Collections and retrieve all Team Projects and output the display name and description of each team project.

CatalogNode catalogNode = configurationServer.CatalogNode;

ReadOnlyCollection<CatalogNode> tpcNodes = catalogNode.QueryChildren(
                new Guid[] { CatalogResourceTypes.ProjectCollection },
                false, CatalogQueryOptions.None);

// tpc = Team Project Collection
foreach (CatalogNode tpcNode in tpcNodes)
{
      Guid tpcId = new Guid(tpcNode.Resource.Properties["InstanceId"]);
      TfsTeamProjectCollection tpc = configurationServer.GetTeamProjectCollection(tpcId);

      // Get catalog of tp = 'Team Projects' for the tpc = 'Team Project Collection'
      var tpNodes = tpcNode.QueryChildren(
                new Guid[] { CatalogResourceTypes.TeamProject },
                false, CatalogQueryOptions.None);

      foreach (var p in tpNodes)
      {
          Debug.Write(Environment.NewLine + " Team Project : " + p.Resource.DisplayName + " - " + p.Resource.Description + Environment.NewLine);
      }
}

 

Output

image

 

You can download a working demo that uses TFS SDK 2010 to programmatically connect to TFS 2010.

Screen Shots of the attached demo application,

image

image

Share this post :
Digg This

© Geeks with Blogs or respective owner