Team Foundation Version Control client API example for TFS 2010 and newer

Buck Hodges

Over six years ago, I posted a sample on how to use the version control API.  The API changed in TFS 2010, but I hadn’t updated the sample.  Here is a version that works with 2010 and newer and is a little less aggressive on clean up in the finally block.

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.

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 add references to the following TFS assemblies to compile this example.

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

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. using Microsoft.TeamFoundation.Client;
  7. using Microsoft.TeamFoundation.VersionControl.Client;
  8.  
  9. namespace BasicSccExample
  10. {
  11.     class Example
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             // Verify that we have the arguments we require.
  16.             if (args.Length < 2)
  17.             {
  18.                 String appName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
  19.                 Console.Error.WriteLine(“Usage: {0} collectionURL teamProjectPath”, appName);
  20.                 Console.Error.WriteLine(“Example: {0} http://tfsserver:8080/tfs/DefaultCollection $/MyProject”, appName);
  21.                 Environment.Exit(1);
  22.             }
  23.  
  24.             // Get a reference to our Team Foundation Server.
  25.             TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(args[0]));
  26.  
  27.             // Get a reference to Version Control.
  28.             VersionControlServer versionControl = tpc.GetService<VersionControlServer>();
  29.  
  30.             // Listen for the Source Control events.
  31.             versionControl.NonFatalError += Example.OnNonFatalError;
  32.             versionControl.Getting += Example.OnGetting;
  33.             versionControl.BeforeCheckinPendingChange += Example.OnBeforeCheckinPendingChange;
  34.             versionControl.NewPendingChange += Example.OnNewPendingChange;
  35.  
  36.             // Create a workspace.
  37.             Workspace workspace = versionControl.CreateWorkspace(“BasicSccExample”, versionControl.AuthorizedUser);
  38.  
  39.             String topDir = null;
  40.  
  41.             try
  42.             {
  43.                 String localDir = @”c:\temp\BasicSccExample”;
  44.                 Console.WriteLine(“\r\n— Create a mapping: {0} -> {1}”, args[1], localDir);
  45.                 workspace.Map(args[1], localDir);
  46.  
  47.                 Console.WriteLine(“\r\n— Get the files from the repository.\r\n”);
  48.                 workspace.Get();
  49.  
  50.                 Console.WriteLine(“\r\n— Create a file.”);
  51.                 topDir = Path.Combine(workspace.Folders[0].LocalItem, “sub”);
  52.                 Directory.CreateDirectory(topDir);
  53.                 String fileName = Path.Combine(topDir, “basic.cs”);
  54.                 using (StreamWriter sw = new StreamWriter(fileName))
  55.                 {
  56.                     sw.WriteLine(“revision 1 of basic.cs”);
  57.                 }
  58.  
  59.                 Console.WriteLine(“\r\n— Now add everything.\r\n”);
  60.                 workspace.PendAdd(topDir, true);
  61.  
  62.                 Console.WriteLine(“\r\n— Show our pending changes.\r\n”);
  63.                 PendingChange[] pendingChanges = workspace.GetPendingChanges();
  64.                 Console.WriteLine(”  Your current pending changes:”);
  65.                 foreach (PendingChange pendingChange in pendingChanges)
  66.                 {
  67.                     Console.WriteLine(”    path: “ + pendingChange.LocalItem +
  68.                                       “, change: “ + PendingChange.GetLocalizedStringForChangeType(pendingChange.ChangeType));
  69.                 }
  70.  
  71.                 Console.WriteLine(“\r\n— Checkin the items we added.\r\n”);
  72.                 int changesetNumber = workspace.CheckIn(pendingChanges, “Sample changes”);
  73.                 Console.WriteLine(”  Checked in changeset “ + changesetNumber);
  74.  
  75.                 Console.WriteLine(“\r\n— Checkout and modify the file.\r\n”);
  76.                 workspace.PendEdit(fileName);
  77.                 using (StreamWriter sw = new StreamWriter(fileName))
  78.                 {
  79.                     sw.WriteLine(“revision 2 of basic.cs”);
  80.                 }
  81.  
  82.                 Console.WriteLine(“\r\n— Get the pending change and check in the new revision.\r\n”);
  83.                 pendingChanges = workspace.GetPendingChanges();
  84.                 changesetNumber = workspace.CheckIn(pendingChanges, “Modified basic.cs”);
  85.                 Console.WriteLine(”  Checked in changeset “ + changesetNumber);
  86.             }
  87.             finally
  88.             {
  89.                 if (topDir != null)
  90.                 {
  91.                     Console.WriteLine(“\r\n— Delete all of the items under the test project.\r\n”);
  92.                     workspace.PendDelete(topDir, RecursionType.Full);
  93.                     PendingChange[] pendingChanges = workspace.GetPendingChanges();
  94.                     if (pendingChanges.Length > 0)
  95.                     {
  96.                         workspace.CheckIn(pendingChanges, “Clean up!”);
  97.                     }
  98.  
  99.                     Console.WriteLine(“\r\n— Delete the workspace.”);
  100.                     workspace.Delete();
  101.                 }
  102.             }
  103.         }
  104.  
  105.         internal static void OnNonFatalError(Object sender, ExceptionEventArgs e)
  106.         {
  107.             if (e.Exception != null)
  108.             {
  109.                 Console.Error.WriteLine(”  Non-fatal exception: “ + e.Exception.Message);
  110.             }
  111.             else
  112.             {
  113.                 Console.Error.WriteLine(”  Non-fatal failure: “ + e.Failure.Message);
  114.             }
  115.         }
  116.  
  117.         internal static void OnGetting(Object sender, GettingEventArgs e)
  118.         {
  119.             Console.WriteLine(”  Getting: “ + e.TargetLocalItem + “, status: “ + e.Status);
  120.         }
  121.  
  122.         internal static void OnBeforeCheckinPendingChange(Object sender, ProcessingChangeEventArgs e)
  123.         {
  124.             Console.WriteLine(”  Checking in “ + e.PendingChange.LocalItem);
  125.         }
  126.  
  127.         internal static void OnNewPendingChange(Object sender, PendingChangeEventArgs e)
  128.         {
  129.             Console.WriteLine(”  Pending “ + PendingChange.GetLocalizedStringForChangeType(e.PendingChange.ChangeType) +
  130.                               ” on “ + e.PendingChange.LocalItem);
  131.         }
  132.     }
  133. }

0 comments

Leave a comment

Feedback usabilla icon