This documentation is archived and is not being maintained.

Connect to Team Foundation Server from a Console Application

You can programmatically connect to a server that is running Team Foundation and then access the team projects on that server if you use the following example. If you modify the example, you can use the services that Getting Additional Team Foundation Services describes later in this topic. You can also act on behalf of others by using impersonation, as Acting on Behalf of Another User (Impersonation) describes later in this topic.

In this topic

You can list the team project collections and the team projects that they contain if you use the following example.

To use this example

  1. Create a C# console application.

  2. Add references to the following assemblies:

  3. Replace the contents of Program.cs with the code that appears later in this topic.

  4. In that code, replace Server, Port, and VDir in the URL that is used to construct the TfsConfigurationServer object so that the URL refers to your server.

    Tip Tip

    To make sure that you are using the correct URL, use Team Explorer to open a team project on your server, and verify the URL properties of the server.

    Team Foundation Server Properties: URL

    Imports System
    Imports System.Collections.ObjectModel
    Imports Microsoft.TeamFoundation.Client
    Imports Microsoft.TeamFoundation.Framework.Common
    Imports Microsoft.TeamFoundation.Framework.Client
    Module Module1
        Sub Main(ByVal sArgs() As String)
            ' Connect to the Team Foundation Server
            ' Server is the name of the server running the application tier for Team Foundation Server
            ' Port is the port that the Team Foundation Server uses. The default port is 8080.
            ' VDir is the virtual path to the Team Foundation application. The default value is tfs.
            Dim tfsUri As Uri
            If sArgs.Length = 0 Then
                tfsUri = New Uri("http://Server:8080/tfs")
                tfsUri = New Uri(sArgs(1))
            End If
            Dim configurationServer As New TfsConfigurationServer(tfsUri)
            configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(tfsUri)
            ' Get the catalog of team project collections
            Dim collectionNodes As ReadOnlyCollection(Of CatalogNode)
            Dim gVar As Guid() = New Guid() {CatalogResourceTypes.ProjectCollection}
            collectionNodes = configurationServer.CatalogNode.QueryChildren(gVar, False, CatalogQueryOptions.None)
            ' List the team project collections
            For Each collectionNode In collectionNodes
                Dim collectionId As Guid = New Guid(collectionNode.Resource.Properties("InstanceID"))
                Dim teamProjectCollection As New TfsTeamProjectCollection(tfsUri)
                teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId)
                System.Console.WriteLine("Collection:" + teamProjectCollection.Name)
                ' Get a catalog of team projects for the collection
                Dim hVar As Guid() = New Guid() {CatalogResourceTypes.TeamProject}
                Dim projectNodes As ReadOnlyCollection(Of CatalogNode)
                projectNodes = collectionNode.QueryChildren(hVar, False, CatalogQueryOptions.None)
                ' List the team projects in the collection
                For Each projectNode In projectNodes
                    System.Console.WriteLine(" Team Project: " + projectNode.Resource.DisplayName)
        End Sub
    End Module

You can access additional services by using one of the GetService methods that the abstract class TfsConnection defines and that TfsConfigurationServer and TfsTeamProjectCollection implement.

When you use the TfsConfigurationServer class, you access the services for the whole server. When you use the TfsTeamProjectCollection class, you access the services for the team project collection. For example, the ITeamFoundationRegistry service for TfsConfigurationServer provides registered properties of the server. The same service that is acquired from TfsTeamProjectCollection provides the registered properties of a team project collection. Some services apply to team project collections only.

When you connect to Team Foundation Server, you can use a method that supports impersonation to act on behalf of an identity other than the one that runs your application. Any operations that are performed based on that connection will be performed on behalf of the impersonated identity. For example, your application could run under the identity of User A but create a connection to Team Foundation Server that impersonates User B. If User A checks in a change to source code under these conditions, the changeset will record that User B checked in the change.

You can use an IdentityDescriptor object when you connect to Team Foundation Server to specify the identity to impersonate. The IdentityDescriptor specifies an identity that Team Foundation defines. When you use this strategy, you do not need to specify a password. The authenticated identity must have the Make requests on behalf of another user permission, except when the authenticated (User A) and impersonated (User B) identities are the same.

You can use an ICredentials object when you connect to Team Foundation Server to specify the identity to impersonate. This strategy does not require special permissions, but you must be able to obtain the password of the identity to create the ICredentials object.

You can also specify an implementation of ICredentialsProvider when you connect to Team Foundation Server to handle requests for new credentials. The system calls the implementation of ICredentialsProvider that you specify to request new credentials when the credentials that are specified by the ICredentials object are not successfully authenticated or authorized to perform the operation.

To prompt the user for credentials, you can use the UICredentialsProvider class, which implements ICredentialsProvider by displaying a logon dialog box to prompt the user for new credentials.

You can use both a Team Foundation identity and authenticated credentials when you connect to Team Foundation Server. For example, your application might run under credentials for User A, but you might use credentials for User B and specify an IdentityDescriptor for User C when you connect to Team Foundation Server. In this case, requests that are made by using that connection are authenticated as User B but performed on behalf of User C. For this strategy to succeed, User B must have the Make requests on behalf of another user permission.