Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

tfs

tfs-sdk

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.

like image 630
John Rasch Avatar asked Dec 01 '09 17:12

John Rasch


People also ask

How do I pull the latest code from TFS?

Right-Click In the File List, right-click the file you want to get and select Source Control > Get Latest Version.

How do I find my Team Foundation Server version?

Right-click on the Microsoft. TeamFoundation. dll file, and select Properties. Under the Version tab, check the value of Product Version in the list box.

How do I download TFS code from Visual Studio 2019?

You can get the TFS project from Visual Studio. First, you have to connect the project via Team Explorer in Visual Studio then add a server and click 'select team project' and you're done. It is downloaded to your PC.


1 Answers

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;
    }
}
like image 170
John Rasch Avatar answered Oct 23 '22 20:10

John Rasch