Search code examples
tfstfvctf-cli

How to work with tfs files from code?


I have a desktop application to modify some xml files that are under source control. The program should be able to get the files, modify them and make a checkin. I already know how to do all that with tf; however, I don't know how to run the developer command prompt for visual studio using code. Also, the program will be installed in computers that doesn't have visual studio; therefore, they won't have the tf command anywhere. Taking into account all that, what would be the best way to run the following commands?

mkdir C:\Temp\PROGRAM
cd C:\Temp\PROGRAM && tf workspace /new /noprompt PROGRAM /collection:"http://myserver:8080/tfs/mycollection"
cd C:\Temp\PROGRAM && tf workfold /map $/my/server/route/to/map C:\Temp\PROGRAM
cd C:\Temp\PROGRAM && tf get

I know that there are some libraries to work with tfvc, but I haven't used them and don't seem as clear as running the commands. Any solution that manage to do the same as the commands above will be welcomed.

  1. Create workspace
  2. Map a folder from the server to a local folder
  3. Download (checkout) all files in that folder (doesn't need to be recursive)

If possible to include in the solution:

  1. Make checkin of files changed

EDIT

The code that I finally used is this

static void Load(string local, string server, Uri urlCollection)
{
    Directory.CreateDirectory(local);
    Workspace workspace = GetWorkspace(urlCollection, "MyWorkspaceName");
    workspace.Map(server, local);
    workspace.Get();
}
static Workspace GetWorkspace(Uri urlCollection, string name)
{
    VersionControlServer vcs = new TfsTeamProjectCollection(urlCollection)
        .GetService<VersionControlServer>();
    try
    { return vcs.GetWorkspace(name, vcs.AuthorizedUser))}
    catch(Exception)
    { return vcs.CreateWorkspace(name, vcs.AuthorizedUser)); }
}


Solution

  • According to your description, you want to pull down files from TFS server and then check in changed files programmatically.

    You could use TFS client API to achieve this. Suggest you go through Buck Hodges's blog which shows how to create a workspace, pend changes, check in those changes.

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

    As for how to get files from TFS, there are also multiple samples in web portal, suggest you use the VersionControlServer.GetItem Method

    Examples of usage:

    // Get the latest Item for local path "C:\projects\myfiles.cs"
    Item item1 = versionControlServer.GetItem("C:\projects\myfiles.cs");
    
    // Get ItemId = 12345 for changesetId = 54321
    Item item2 = versionControlServer.GetItem(12345,54321);
    
    // Get the latest Item for server path "$/ProjectName/myfile.cs"
    Item item1 = versionControlServer.GetItem("$/ProjectName/myfile.cs", VersionSpec.Latest);
    

    You could also select a history version of source code to pull down, for the entire code, please refer below tutorials: Team Foundation Server API: Programmatically Downloading Files From Source Control

    Another way is using powershell script to handle this, please take a look here: Download files from TFS server with PowerShell