简体   繁体   中英

How do you get the latest version of source code using the Team Foundation Server 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.

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;
    }
}

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;
            }
        }
    }

Your approach is valid.

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

string projectPath = "$/PathToApp/TestApp";

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

I had a similar situation where I needed to download contents of 'a' folder from tfs into an existing workspace, without creating a new workspace. With help from the above answers, I was able to put something together that works fine for me as of now. There is however a limitation. This works for contents of 'a' folder with just files and not another folder inside it -I have not tried that out. Maybe that would involve some minor updates. Sharing code, just in case someone is searching for this. I really like the fact that this approach does not deal with workspace [ -create and delete ], since that is not desired.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Client;
using System.IO;

namespace DownloadFolder
{
    class Program
    {
        static void Main(string[] args)
        {
            string teamProjectCollectionUrl = "http://<YourTFSUrl>:8080/tfs/DefaultCollection";  // Get the version control server
            TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(teamProjectCollectionUrl));
            VersionControlServer vcs = teamProjectCollection.GetService<VersionControlServer>();
            String Sourcepath = args[0]; // The folder path in TFS - "$/<TeamProject>/<FirstLevelFolder>/<SecondLevelFolder>"
            String DestinationPath = args[1]; //The folder in local machine - "C:\MyTempFolder"
            ItemSet items = vcs.GetItems(Sourcepath, VersionSpec.Latest, RecursionType.Full);
            String FolderName = null;
            foreach (Item item in items.Items)
            {
                String ItemName = Path.GetFileName(item.ServerItem);
                switch (item.ItemType)
                {
                    case ItemType.File:                        
                        item.DownloadFile(Path.Combine(DestinationPath, FolderName, ItemName));
                        break;
                    case ItemType.Folder:
                        FolderName = Path.GetFileName(item.ServerItem);
                        Directory.CreateDirectory(Path.Combine(DestinationPath, ItemName));
                        break;
                }
            }
        }
    }
}

While running this from the command prompt copy all the supporting dlls along with exe cmd>> DownloadFolder.exe "$/<TeamProject>/<FirstLevelFolder>/<SecondLevelFolder>" "C:\\MyTempFolder"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM