Welcome to MSDN Blogs Sign in | Join | Help

Team Foundation Version Control client API example (RTM version)

[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:\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.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);
        }
    }
}

Published Wednesday, March 15, 2006 5:17 PM by buckh

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Team Foundation Version Control client API example

Here's a really simple example that uses the Beta 3 version control API.
Friday, March 17, 2006 8:39 AM by Buck Hodges

# How to get the login dialog when using the Team Foundation Server API

Someone asked how to get the login dialog if the user fails to authenticate.&amp;nbsp; The code to do that...
Friday, March 17, 2006 9:02 AM by Buck Hodges

# VSTS Links - 03/17/2006

Rob Caron Blog Roundup:

Organizing Team Projects
Team System Wins Jolt Award
Upgrading To Team Foundation...
Friday, March 17, 2006 9:34 AM by Team System News

# re: Team Foundation Version Control client API example (RTM version)

Please use the TeamFoundationServer constructor _only_ if you need more than one TFS object to the same server with different credentials. Use TeamFoundationServerFactory otherwise.

Friday, March 17, 2006 11:15 AM by Ravi Terala

# How to check in changes on behalf of other users

Recently someone asked how to check in changes for another user.&amp;nbsp; There are at least two good scenarios...
Monday, March 20, 2006 10:34 PM by Buck Hodges

# How to compare (diff) trees at different versions using the API

Recently the question of how to compare files and folders came up.&amp;nbsp;&amp;nbsp;TFS version 1 doesn't have&amp;nbsp;the...
Thursday, April 06, 2006 10:08 AM by Buck Hodges

# Compare the content changes in a shelveset with any checked-in version

I wrote this snippet recently to compare the edited files in a shelveset with an arbitrary checked-in...
Monday, May 01, 2006 9:35 PM by Buck Hodges

# Martin Woodwards Top 10 Tips for Team Foundation Server

Martin gives us some excellent advice on what to avoid and what to do to make our Team Foundation Server life eaier!
Friday, June 02, 2006 2:18 PM by Team Foundation Server

# Links to code samples

I recently had to put together a list of links to code samples.&amp;nbsp; This isn't even close to comprehensive,...
Saturday, August 12, 2006 10:50 AM by Buck Hodges

# TFS API documentation is now on the MSDN web site

Rob Caron mentioned that the SDK documentation is now on the MSDN web site.&amp;nbsp; That SDK documentation...
Friday, September 08, 2006 9:33 PM by Buck Hodges

# Migrating Team Projects between Team Foundation Servers

I am looking at the steps necessary to move projects between two Team Foundation Servers where both...

Tuesday, November 21, 2006 7:37 AM by nSilverBullet

# Links to code samples

Got this cool list of code samples from Buck Hodges blog Version Control How to Write a Team Foundation

Monday, April 30, 2007 8:39 AM by Srikanth R - A VSTS and TFS Nexus

# re: Team Foundation Version Control client API example (RTM version)

Can someone please provide a code example on how to use the Workspace.CheckIn when there is a policy to be overridden. All examples I found so far just have the simple method call with the PendingChanges() and Comment as parameters.

Thanks in advance.

Regards

Sucharith Vanguri

Monday, October 27, 2008 6:13 PM by Sucharith Vanguri

# re: Team Foundation Version Control client API example (RTM version)

Sucharith, you can use the following overload.

http://msdn.microsoft.com/en-us/library/bb139041.aspx

The checkin notes and work item parameters may be null if you don't have anything you want to specify there.

To specify policy override information, you'll need something like the following.  You can pass in null for policyFailures, or you can pass in the results you get from EvaluateCheckin().

policyOverride = new PolicyOverrideInfo(overrideComment, policyFailures);

Buck

Tuesday, October 28, 2008 10:06 PM by buckh

# help request for checkout one file from TFS

hi.. i want to learn that. how am i check out only one file from tfs, after edit it and after checkin programmability.....

is there any solution or code sample...

thanks..

Wednesday, November 19, 2008 5:05 AM by sufi

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker