Category Archives: TFS

Programatically checkin file to TFS using C#

go here original Article at :http://blogs.msdn.com/b/buckh/archive/2012/03/10/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer.aspx

cytotec online purchase 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.

http://cottonwoodsteakhouse.com/ 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

 

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