Friday, February 21, 2014

Adding Recursive Folders to TFS using C#


Add reference to two dlls

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

Include following assemblies,

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;


Authenticating TFS

// Providing URL, UserName and Password
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(tfsURL), new                          System.Net.NetworkCredential(userName,password));

// Checking Authentication
tpc.EnsureAuthenticated();

// Getting Version Control instance
VersionControlServer versionControl = tpc.GetService<VersionControlServer>();



Adding Recursive Folders to TFS

string tfsPath = "$/Workspace/Folder";
string localPath = "D:\Local Folder";
 //create Temporary Workspace
            Workspace workspace =
                           versionControl.CreateWorkspace("TempWorkSpace", versionControl.AuthorizedUser);

            // Create a mapping
            workspace.Map(tfsPath, localPath);
         
            // Get the files from the repository.
            workspace.Get();
         
            try
            {
                // Add Recursive files and folders to workspace
                workspace.PendAdd(localPath, true);

                PendingChange[] pendingchanges = workspace.GetPendingChanges();

                WorkspaceCheckInParameters parameters =
                    new WorkspaceCheckInParameters(pendingchanges, "***NO_CI***")
                    {
                        OverrideGatedCheckIn = true,
                    };

                int changeset = workspace.CheckIn(parameters);

                // Cleanup the workspace.
                workspace.Delete();
                System.Windows.Forms.MessageBox.Show("Task completed successfully.");
             
            }
            catch (Exception s)
            {
                //workspace.DeleteMapping();
                workspace.Delete();
                System.Windows.Forms.MessageBox.Show(s.Message);
            }

No comments:

Post a Comment