{"id":5333,"date":"2005-01-22T11:46:00","date_gmt":"2005-01-22T11:46:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/buckh\/2005\/01\/22\/team-foundation-source-control-client-api-example\/"},"modified":"2005-01-22T11:46:00","modified_gmt":"2005-01-22T11:46:00","slug":"team-foundation-source-control-client-api-example","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/buckh\/team-foundation-source-control-client-api-example\/","title":{"rendered":"Team Foundation Source Control client API example"},"content":{"rendered":"<p><strong>This blog post is obsolete. Please see\u00a0<a href=\"https:\/\/devblogs.microsoft.com\/buckh\/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer\/\">https:\/\/devblogs.microsoft.com\/buckh\/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer\/<\/a><\/strong><\/p>\n<p>Here&#8217;s a really simple example that uses the source control API () in the Dec. CTP.\u00a0 It shows how to create a workspace, pend changes, check in those changes, and hook up some important event listeners.\u00a0 This sample doesn&#8217;t do anything useful, but it should get you going.<\/p>\n<p dir=\"ltr\" style=\"margin-right: 0px\">You&#8217;ll need to reference the following dlls to compile this example.\nSystem.dll\nMicrosoft.VisualStudio.Hatteras.Client.dll\nMicrosoft.VisualStudio.TeamFoundation.Client.dll<\/p>\n<p>If you write something with the API, please <a href=\"http:\/\/blogs.msdn.com\/buckh\/contact.aspx\">send me email<\/a> or leave a comment.<\/p>\n<p>using System;\nusing System.Collections.Generic;\nusing System.IO;\nusing System.Text;\nusing Microsoft.VisualStudio.Hatteras.Client;\nusing Microsoft.VisualStudio.TeamFoundation.Client;<\/p>\n<p>namespace BasicSccExample\n{\nclass Example\n{\nstatic void Main(string[] args)\n{\n\/\/ Verify that we have the arguments we require.\nif (args.Length &lt; 1)\n{\nConsole.Error.WriteLine(&#8220;Usage: app tfsName&#8221;);\nEnvironment.Exit(1);\n}<\/p>\n<p>\/\/ Get a reference to our Team Foundation Server.\nString tfsName = args[0];\nTeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(tfsName);<\/p>\n<p>\/\/ Get a reference to Source Control.\nSourceControl sourceControl = (SourceControl) tfs.GetService(typeof(SourceControl));<\/p>\n<p>\/\/ Listen for the Source Control events.\nsourceControl.NonFatalError += Example.OnNonFatalError;\nsourceControl.Getting += Example.OnGetting;\nsourceControl.BeforeCheckinPendingChange += Example.OnBeforeCheckinPendingChange;\nsourceControl.NewPendingChange += Example.OnNewPendingChange;<\/p>\n<p>\/\/ Create a workspace.\nWorkspace workspace = sourceControl.CreateWorkspace(&#8220;BasicSccExample&#8221;, sourceControl.AuthenticatedUser);<\/p>\n<p>try\n{\n\/\/ Create a mapping.\nworkspace.CreateMapping(&#8220;$\/&#8221;, @&#8221;c:\\temp\\BasicSccExample&#8221;);<\/p>\n<p>\/\/ Get the files from the repository.\nworkspace.Get();<\/p>\n<p>\/\/ Create a file.\nString topDir = Path.Combine(workspace.Folders[0].LocalItem, &#8220;sub&#8221;);\nDirectory.CreateDirectory(topDir);\nString fileName = Path.Combine(topDir, &#8220;basic.cs&#8221;);\nusing (StreamWriter sw = new StreamWriter(fileName))\n{\nsw.WriteLine(&#8220;revision 1 of basic.cs&#8221;);\n}<\/p>\n<p>\/\/ Now add everything.\nworkspace.PendAdd(topDir, true);<\/p>\n<p>\/\/ Show our pending changes.\nPendingChange[] pendingChanges = workspace.GetPendingChanges();\nConsole.WriteLine(&#8220;Your current pending changes:&#8221;);\nforeach (PendingChange pendingChange in pendingChanges)\n{\nConsole.WriteLine(&#8221;\u00a0 path: &#8221; + pendingChange.LocalItem +\n&#8220;, change: &#8221; + PendingChange.ChangeTypeToString(pendingChange.ChangeType));\n}<\/p>\n<p>\/\/ Checkin the items we added.\nint changesetNumber = workspace.CheckIn(pendingChanges, &#8220;Sample changes&#8221;);\nConsole.WriteLine(&#8220;Checked in changeset &#8221; + changesetNumber);<\/p>\n<p>\/\/ Checkout and modify the file.\nworkspace.PendEdit(fileName);\nusing (StreamWriter sw = new StreamWriter(fileName))\n{\nsw.WriteLine(&#8220;revision 2 of basic.cs&#8221;);\n}<\/p>\n<p>\/\/ Get the pending change and check in the new revision.\npendingChanges = workspace.GetPendingChanges();\nchangesetNumber = workspace.CheckIn(pendingChanges, &#8220;Modified basic.cs&#8221;);\nConsole.WriteLine(&#8220;Checked in changeset &#8221; + changesetNumber);\n}\nfinally\n{\n\/\/ Delete the items.\nworkspace.PendDelete(&#8220;$\/*&#8221;, RecursionType.Full);\nPendingChange[] pendingChanges = workspace.GetPendingChanges();\nif (pendingChanges.Length &gt; 0)\n{\nworkspace.CheckIn(pendingChanges, &#8220;Clean up!&#8221;);\n}<\/p>\n<p>\/\/ Delete the workspace.\nworkspace.Delete();\n}\n}<\/p>\n<p>internal static void OnNonFatalError(Object sender, ExceptionEventArgs e)\n{\nif (e.Exception != null)\n{\nConsole.Error.WriteLine(&#8220;Non-fatal exception: &#8221; + e.Exception.Message);\n}\nelse\n{\nConsole.Error.WriteLine(&#8220;Non-fatal failure: &#8221; + e.Failure.Message);\n}\n}<\/p>\n<p>internal static void OnGetting(Object sender, GettingEventArgs e)\n{\nConsole.WriteLine(&#8220;Getting: &#8221; + e.TargetLocalItem + &#8220;, status: &#8221; + e.Status);\n}<\/p>\n<p>internal static void OnBeforeCheckinPendingChange(Object sender, PendingChangeEventArgs e)\n{\nConsole.WriteLine(&#8220;Checking in &#8221; + e.PendingChange.LocalItem);\n}<\/p>\n<p>internal static void OnNewPendingChange(Object sender, PendingChangeEventArgs e)\n{\nConsole.WriteLine(&#8220;Pending &#8221; + PendingChange.ChangeTypeToString(e.PendingChange.ChangeType) +\n&#8221; on &#8221; + e.PendingChange.LocalItem);\n}\n}\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post is obsolete. Please see\u00a0https:\/\/devblogs.microsoft.com\/buckh\/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer\/ Here&#8217;s a really simple example that uses the source control API () in the Dec. CTP.\u00a0 It shows how to create a workspace, pend changes, check in those changes, and hook up some important event listeners.\u00a0 This sample doesn&#8217;t do anything useful, but it should get you going. [&hellip;]<\/p>\n","protected":false},"author":94,"featured_media":10268,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[6,8],"class_list":["post-5333","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-source-control","tag-team-foundation"],"acf":[],"blog_post_summary":"<p>This blog post is obsolete. Please see\u00a0https:\/\/devblogs.microsoft.com\/buckh\/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer\/ Here&#8217;s a really simple example that uses the source control API () in the Dec. CTP.\u00a0 It shows how to create a workspace, pend changes, check in those changes, and hook up some important event listeners.\u00a0 This sample doesn&#8217;t do anything useful, but it should get you going. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/5333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/users\/94"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/comments?post=5333"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/5333\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/media\/10268"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/media?parent=5333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/categories?post=5333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/tags?post=5333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}