How do you get the latest version of source code using the Team Foundation Server SDK?

29,102

Solution 1

I ended up using a different approach that seems to work, mainly taking advantage of the Item.DownloadFile() method:

VersionControlServer sourceControl; // actually instantiated...

ItemSet items = sourceControl.GetItems(sourcePath, VersionSpec.Latest, RecursionType.Full);

foreach (Item item in items.Items)
{
    // build relative path
    string relativePath = BuildRelativePath(sourcePath, item.ServerItem);

    switch (item.ItemType)
    {
    case ItemType.Any:
        throw new ArgumentOutOfRangeException("ItemType returned was Any; expected File or Folder.");
    case ItemType.File:
        item.DownloadFile(Path.Combine(targetPath, relativePath));
        break;
    case ItemType.Folder:
        Directory.CreateDirectory(Path.Combine(targetPath, relativePath));
        break;
    }
}

Solution 2

I completed and implemented the code into a button as web asp.net solution.

For the project to work in the references should be added the Microsoft.TeamFoundation.Client and Microsoft.TeamFoundation.VersionControl.Client references and in the code the statements using Microsoft.TeamFoundation.Client; and using Microsoft.TeamFoundation.VersionControl.Client;

    protected void Button1_Click(object sender, EventArgs e)
    {
        string workspaceName = "MyWorkspace";
        string projectPath = @"$/TeamProject"; // the container Project (like a tabel in sql/ or like a folder) containing the projects sources in a collection (like a database in sql/ or also like a folder) from TFS

        string workingDirectory = @"D:\New1";  // local folder where to save projects sources

        TeamFoundationServer tfs = new TeamFoundationServer("http://test-server:8080/tfs/CollectionName", System.Net.CredentialCache.DefaultCredentials);
                                                            // tfs server url including the  Collection Name --  CollectionName as the existing name of the collection from the tfs server 
        tfs.EnsureAuthenticated(); 

        VersionControlServer sourceControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));

        Workspace[] workspaces = sourceControl.QueryWorkspaces(workspaceName, sourceControl.AuthenticatedUser, Workstation.Current.Name);
        if (workspaces.Length > 0)
        {
            sourceControl.DeleteWorkspace(workspaceName, sourceControl.AuthenticatedUser);
        }
        Workspace workspace = sourceControl.CreateWorkspace(workspaceName, sourceControl.AuthenticatedUser, "Temporary Workspace");
        try
        {
            workspace.Map(projectPath, workingDirectory);
            GetRequest request = new GetRequest(new ItemSpec(projectPath, RecursionType.Full), VersionSpec.Latest);
            GetStatus status = workspace.Get(request, GetOptions.GetAll | GetOptions.Overwrite); // this line doesn't do anything - no failures or errors
        }
        finally
        {
            if (workspace != null)
            {
                workspace.Delete();
                Label1.Text = "The Projects have been brought into the Folder  " + workingDirectory;
            }
        }
    }

Solution 3

Your approach is valid.

Your error is in your project path. Use something like this instead:

string projectPath = "$/PathToApp/TestApp";

Solution 4

I agree with Joerage that your server path is probably the culprit. To get more insight into what's happening, you need to wire up some events on the VersionControlServer object. At minimum you'll want Getting, NonFatalError, and Conflict.

Complete list: http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.versioncontrolserver_events(VS.80).aspx

Share:
29,102
Summer Sun
Author by

Summer Sun

Updated on September 21, 2020

Comments

  • Summer Sun
    Summer Sun over 3 years

    I'm attempting to pull the latest version of source code out of TFS programmatically using the SDK, and what I've done somehow does not work:

    string workspaceName = "MyWorkspace";
    string projectPath = "/TestApp";
    string workingDirectory = "C:\Projects\Test\TestApp";
    
    VersionControlServer sourceControl; // actually instantiated before this method...
    
    Workspace[] workspaces = sourceControl.QueryWorkspaces(workspaceName, sourceControl.AuthenticatedUser, Workstation.Current.Name);
    if (workspaces.Length > 0)
    {
        sourceControl.DeleteWorkspace(workspaceName, sourceControl.AuthenticatedUser);
    }
    Workspace workspace = sourceControl.CreateWorkspace(workspaceName, sourceControl.AuthenticatedUser, "Temporary Workspace");
    try
    {
        workspace.Map(projectPath, workingDirectory);
        GetRequest request = new GetRequest(new ItemSpec(projectPath, RecursionType.Full), VersionSpec.Latest);
        GetStatus status = workspace.Get(request, GetOptions.GetAll | GetOptions.Overwrite); // this line doesn't do anything - no failures or errors
    }
    finally
    {
        if (workspace != null)
        {
            workspace.Delete();
        }
    }
    

    The approach is basically creating a temporary workspace, using the Get() method to grab all the items for this project, and then removing the workspace. Is this the correct way to do this? Any examples would be helpful.