Team Foundation Source Control client API example


Here’s a really simple example that uses the source control API () in the Dec. CTP.  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’ll need to reference the following dlls to compile this example.
System.dll
Microsoft.VisualStudio.Hatteras.Client.dll
Microsoft.VisualStudio.TeamFoundation.Client.dll

If you write something with the API, please send me email or leave a comment.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.VisualStudio.Hatteras.Client;
using Microsoft.VisualStudio.TeamFoundation.Client;

namespace BasicSccExample
{
    class Example
    {
        static void Main(string[] args)
        {
            // Verify that we have the arguments we require.
            if (args.Length < 1)
            {
                Console.Error.WriteLine("Usage: app tfsName");
                Environment.Exit(1);
            }

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

            // Get a reference to Source Control.
            SourceControl sourceControl = (SourceControl) tfs.GetService(typeof(SourceControl));

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

            // Create a workspace.
            Workspace workspace = sourceControl.CreateWorkspace("BasicSccExample", sourceControl.AuthenticatedUser);

            try
            {
                // Create a mapping.
                workspace.CreateMapping("$/", @"c:\temp\BasicSccExample");

                // 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.ChangeTypeToString(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 the items.
                workspace.PendDelete("$/*", 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, PendingChangeEventArgs e)
        {
            Console.WriteLine("Checking in " + e.PendingChange.LocalItem);
        }

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

Comments (16)

  1. Anonymous says:

    Getting Started with the Team Foundation Source Control API

  2. Keith Hill says:

    I like it. It seems that mere mortals can actually program against this API. Thumbs up WRT what I’ve seen so far.

  3. Buck Hodges says:

    Here’s a really simple example that uses the Beta 3 version control API.

  4. Buck Hodges says:

    Here’s a really simple example that uses the Beta 3 version control API.

  5. Buck Hodges says:

    Here’s a really simple example that uses the Beta 3 version control API.

  6. Buck Hodges says:

    Here’s a really simple example that uses the Beta 3 version control API.

  7. Damir Kovac says:

    Simple, yet powerful example, keep up the good work!

  8. TestUser says:

    Hi,

    can you tell me where I get the file  Microsoft.VisualStudio.TeamFoundation.Client.dll, please?

    I’m working with the vm that includes the tfs 2010 beta and the visual studio 2010 beta 2.

    Thanks.

  9. buckh says:

    TestUser, you will find that one and the others that are in the GAC in %ProgramFiles%Microsoft Visual Studio 10.0Common7IDEReferenceAssembliesv2.0.  While I’m using the RC installed on my machine to answer this, I believe the answer should be the same for beta 2.

    Buck

  10. slasalle says:

    Can anyone give me an example of how to use the workspace.getPendingChanges(string) method? I'm trying to only get pending changes for a specific branch in my source control, and it seem like that's the ticket but I can't seem to get it going.

  11. slasalle says:

    probably should have mentioned, working with tfs2010

  12. buckh says:

    slasalle, you'll actually want the one that takes a string (path) and a recursion type: msdn.microsoft.com/…/bb139286.aspx.  In your case, it sounds like you want RecursionType.Full since you want all changes in your path (the branch).

    Buck

  13. Henry says:

    could Microsoft.VisualStudio.TeamFoundation.Client.dll be used without license issue in application development?

  14. buckh says:

    Henry, I'm not sure.  I'd recommend checking out the licensing whitepaper at http://www.microsoft.com/…/details.aspx.

    Buck