Team Foundation Source Control client API example

Buck Hodges

This blog post is obsolete. Please see https://devblogs.microsoft.com/buckh/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer/

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); } } }

0 comments

Leave a comment

Feedback usabilla icon