How to: Use the TFS SDK to Write Code

The following procedure shows you how to write code with the Team Foundation Server SDK. It shows you how to create the project, add the appropriate assemblies, and then compile and run the code.

To Create the Project

  1. Start Visual Studio.

  2. Do one of the following:

    • On the File menu, click New. Then click New Project.

    • Press Ctrl+Shift+N, and the New Project dialog box will appear.

  3. In Project types, expand the Visual C# node. Then select Windows.

  4. In Templates, select Console Application.

  5. In Name, type TfsApplication1.

  6. Click OK.

To Add the Appropriate Assemblies

  1. On the Project menu, click Add Reference.

  2. In the Add Reference dialog box, click the .NET tab.

  3. Select Microsoft.TeamFoundation.Client, and then Microsoft.TeamFoundation.WorkItemTracking.Client.

    If these assemblies are not listed, click the Browse tab to locate these assemblies. Add them to the project. The assemblies should be in the Windows directory, in the "assembly" folder.

  4. Click OK.

To Compile and Run the Code

  1. Copy and paste the example code to replace the default code in Program.cs

  2. Do one of the following:

    • On the Debug menu, click Start Without Debugging.

    • Press Ctrl+F5.

  3. When prompted, enter the friendly name or URI of a valid Team Foundation Server.

Example

This Code Example will connect to the Team Foundation Server, connect to the WorkItemStore, and then list the projects available on the Team Foundation Server.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TfsApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Let the user choose a TFS Server.
            Console.Write("Please enter a valid TFS Server or URI: ");
            String tfsServer = Console.ReadLine();
            tfsServer = tfsServer.Trim();

            // Connect to the TeamFoundationServer.
            Console.WriteLine();
            Console.Write("Connecting to Team Foundation Server {0}...", tfsServer);
            TeamFoundationServer tfs =
                TeamFoundationServerFactory.GetServer(tfsServer);

            // Connect to the WorkItemStore.
            Console.WriteLine();
            Console.Write("Reading from the Work Item Store...");
            WorkItemStore workItemStore = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));

            // Display the details about the TeamFoundationServer.
            Console.WriteLine("\n");
            Console.WriteLine("Team Foundation Server details");
            Console.WriteLine("Server Name: " + tfs.Name);
            Console.WriteLine("Uri: " + tfs.Uri);
            Console.WriteLine("AuthenticatedUserDisplayName: " + tfs.AuthenticatedUserDisplayName);
            Console.WriteLine("AuthenticatedUserName: " + tfs.AuthenticatedUserName);
            Console.WriteLine("WorkItemStore:");

            //  List the Projects in the WorkItemStore.
            Console.WriteLine("  Projects.Count: " + workItemStore.Projects.Count);
            foreach (Project pr in workItemStore.Projects)
            {
                Console.WriteLine("    " + pr.Name);
            }
        }
    }
}

See Also

Tasks

How to: Run Team Foundation Server SDK Samples

Concepts

Work Item Tracking Architecture