trinityalps
trinityalps

Reputation: 397

Team Foundation get workspace error

I am trying to get the local workspace to sync up with the remote workspace. The remote workspace is WindowsMain/MainProject/Subproject and the local is C:/MainProject/Subproject the problem is when I run the code shown below it says that the local workspace isn't mapped. What am I doing wrong that makes it so that the code isn't found? (localpath is C:/MainProject/Subproject

TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://remoteserver:8080/tfs_proj/WindowsMain"), new UICredentialsProvider()); tfs.EnsureAuthenticated(); VersionControlServer vsStore = tfs.GetService<VersionControlServer>(); Workspace workspace = vsStore.GetWorkspace(localpath);

Upvotes: 1

Views: 595

Answers (2)

NSGaga
NSGaga

Reputation: 14312

I'm not sure what is that you want exactly,
...but this is the code I used (and I remember having trouble finding the right solution at the time):

teamProjectCollection = new TfsTeamProjectCollection(new Uri(collectionUri), new TfsClientCredentials());
teamProjectCollection.Authenticate();
versionControl = teamProjectCollection.GetService<VersionControlServer>();  

(note: the credentials like this are automatically picked from what you use in VS)

then I get all the workspaces (in your case it may be just one) - that helps to get the right matching paths for server and client (the Workstation class does the same, if that doesn't work for some reason, this does)

versionControl.QueryWorkspaces(null, null, computerName)
    .SelectMany(x => x.Folders); // returns IEnumerable<WorkingFolder>  

pick the workspace you need and you have both local and server path

LocalSourcePath = folder.LocalItem;
ServerPath = folder.ServerItem;  

...and then I use the ServerPath and the versionControl to actually download the files (and e.g. you can check them against the LocalSourcePath)...

foreach (Item item in
    versionControl.GetItems(serverPath, VersionSpec.Latest, RecursionType.Full, DeletedState.NonDeleted, ItemType.Any, true).Items)
{
    string target = Path.Combine(downloadPath, item.ServerItem.Substring(2));
    if (item.ItemType == ItemType.Folder && !Directory.Exists(target))
    {
        Directory.CreateDirectory(target);
    }
    else if (item.ItemType == ItemType.File)
    {
        item.DownloadFile(target);
    }
}  

...this is probably slightly different for your case but you should be able to work out the specific details from this, hope it helps.

Upvotes: 2

PatrickLu-MSFT
PatrickLu-MSFT

Reputation: 51183

According to you code, you need to pass through a VersionControlServer object, and to obtain such an object you need to know the address of the Tfs server the workspace is mapped to.

If you can determine the physical path of the solution or project file, then you can query that file in TFS and you should see which workspace has been mapped to that local file location. Note: The mapped path for a workspace had to be unique.

Moreover, there is a more powerful way though, using the Workstation class. Ricci Gian Maria has written a quite extensive blog post about this topic.

Use the Workstation class to get the WorkspaceInfo for the path you're looking for, this will search the workpaces for all TFS servers registered on that workstation to see if there's a match:

Workstation workstation = Workstation.Current;
WorkspaceInfo info = workstation.GetLocalWorkspaceInfo(path);

More details please refer his blog: How to get TFS server address from a local folder mapped to a workspace

Upvotes: 1

Related Questions