Team Foundation Version Control client API example (RTM version)

Buck Hodges

[Update 3/10/2012] If you are using TFS 2010 or newer, there is an updated version control client API example.

[Update 6/13/06] While the documentation is not what it needs to be, you can find reference-style documentation on a significant amount of the API in the VS SDK (current release is April): http://blogs.msdn.com/buckh/archive/2005/12/09/502179.aspx.

I’ve updated this sample a few times before.  This is a really simple example that uses the version control API.  It shows how to create a workspace, pend changes, check in those changes, and hook up some important event listeners.  This sample doesn’t do anything useful, but it should get you going.

You have to supply a Team Project as an argument.  Note that it deletes everything under the specified Team Project, so don’t use this on a Team Project or server you care about.

The only real difference in this version is that it uses the TeamFoundationServer constructor (in beta 3, you were forced to use the factory class).

You’ll need to reference the following dlls to compile this example.

System.dll
Microsoft.TeamFoundation.VersionControl.Client.dll
Microsoft.TeamFoundation.Client.dll

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace BasicSccExample
{
    class Example
    {
        static void Main(string[] args)
        {
            // Verify that we have the arguments we require.
            if (args.Length < 2)
            {
                String appName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
                Console.Error.WriteLine(“Usage: {0} tfsServerName tfsTeamProjectPath”, appName);
                Console.Error.WriteLine(“Example: {0} http://tfsserver:8080 $/MyProject”, appName);
                Environment.Exit(1);
            }

            // Get a reference to our Team Foundation Server.
            String tfsName = args[0];
            TeamFoundationServer tfs = new TeamFoundationServer(tfsName);

            // Get a reference to Version Control.
            VersionControlServer versionControl = (VersionControlServer) tfs.GetService(typeof(VersionControlServer));

            // Listen for the Source Control events.
            versionControl.NonFatalError += Example.OnNonFatalError;
            versionControl.Getting += Example.OnGetting;
            versionControl.BeforeCheckinPendingChange += Example.OnBeforeCheckinPendingChange;
            versionControl.NewPendingChange += Example.OnNewPendingChange;

            // Create a workspace.
            Workspace workspace = versionControl.CreateWorkspace(“BasicSccExample”, versionControl.AuthenticatedUser);

            try
            {
                // Create a mapping using the Team Project supplied on the command line.
                workspace.Map(args[1], @”c:tempBasicSccExample”);

                // Get the files from the repository.
                workspace.Get();

                // Create a file.
                String topDir = Path.Combine(workspace.Folders[0].LocalItem, “sub”);
                Directory.CreateDirectory(topDir);
                String fileName = Path.Combine(topDir, “basic.cs”);
                using (StreamWriter sw = new StreamWriter(fileName))
                {
                    sw.WriteLine(“revision 1 of basic.cs”);
                }

                // Now add everything.
                workspace.PendAdd(topDir, true);

                // Show our pending changes.
                PendingChange[] pendingChanges = workspace.GetPendingChanges();
                Console.WriteLine(“Your current pending changes:”);
                foreach (PendingChange pendingChange in pendingChanges)
                {
                    Console.WriteLine(”  path: “ + pendingChange.LocalItem +
                                      “, change: “ + PendingChange.GetLocalizedStringForChangeType(pendingChange.ChangeType));
                }

                // Checkin the items we added.
                int changesetNumber = workspace.CheckIn(pendingChanges, “Sample changes”);
                Console.WriteLine(“Checked in changeset “ + changesetNumber);

                // Checkout and modify the file.
                workspace.PendEdit(fileName);
                using (StreamWriter sw = new StreamWriter(fileName))
                {
                    sw.WriteLine(“revision 2 of basic.cs”);
                }

                // Get the pending change and check in the new revision.
                pendingChanges = workspace.GetPendingChanges();
                changesetNumber = workspace.CheckIn(pendingChanges, “Modified basic.cs”);
                Console.WriteLine(“Checked in changeset “ + changesetNumber);
            }
            finally
            {
                // Delete all of the items under the test project.
                workspace.PendDelete(args[1], RecursionType.Full);
                PendingChange[] pendingChanges = workspace.GetPendingChanges();
                if (pendingChanges.Length > 0)
                {
                    workspace.CheckIn(pendingChanges, “Clean up!”);
                }

                // Delete the workspace.
                workspace.Delete();
            }
        }

        internal static void OnNonFatalError(Object sender, ExceptionEventArgs e)
        {
            if (e.Exception != null)
            {
                Console.Error.WriteLine(“Non-fatal exception: “ + e.Exception.Message);
            }
            else
            {
                Console.Error.WriteLine(“Non-fatal failure: “ + e.Failure.Message);
            }
        }

        internal static void OnGetting(Object sender, GettingEventArgs e)
        {
            Console.WriteLine(“Getting: “ + e.TargetLocalItem + “, status: “ + e.Status);
        }

        internal static void OnBeforeCheckinPendingChange(Object sender, ProcessingChangeEventArgs e)
        {
            Console.WriteLine(“Checking in “ + e.PendingChange.LocalItem);
        }

        internal static void OnNewPendingChange(Object sender, PendingChangeEventArgs e)
        {
            Console.WriteLine(“Pending “ + PendingChange.GetLocalizedStringForChangeType(e.PendingChange.ChangeType) +
                              ” on “ + e.PendingChange.LocalItem);
        }
    }
}

0 comments

Leave a comment

Feedback usabilla icon