Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call GetWorkspace in TFS properly?

Tags:

c#

tfs

Currently when I call GetWorkspace I get ItemNotMappedException exception, but when I iterate over workspaces manually I can get my code working. This is so bizarre that I am wondering if I should call some refresh or something before I call GetWorkspace?

Here is my code:

Workspace active = null;
using (var tfs = new TfsTeamProjectCollection(uri))
{
    tfs.EnsureAuthenticated();
    VersionControlServer vcs = tfs.GetService<VersionControlServer>();

    // displaying info and manually finding desired workspace
    foreach (var ws in vcs.QueryWorkspaces(null, vcs.AuthorizedUser, Environment.MachineName))
    {
        Console.WriteLine(ws.Name);
        foreach (var f in ws.Folders)
        {
            Console.WriteLine($"  {f.LocalItem}");
            if (f.LocalItem == map_path)
                active = ws;
        }
    }

    // at this point workspace is found and I can work with it

    // but this will crash
    Workspace workspace = vcs.GetWorkspace(map_path);        
}

I use VS2015 and the library for TFS is fetched from NuGet repo. It is "Microsoft.TeamFoundationServer.ExtendedClient" version "15.112.1".

like image 220
astrowalker Avatar asked Sep 29 '17 07:09

astrowalker


1 Answers

The VersionControlServer.GetWorkspace method searches all known workspaces on the current computer to identify a workspace that has explicitly or implicitly mapped the provided local path. If no workspace is found, this method throws an ItemNotMappedException.

Try to call to Workstation.EnsureUpdateWorkspaceInfoCache in your code to force update the workspace information cache:

TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("<your-tfs-uri-here>"));
VersionControlServer tfServer = tpc.GetService<VersionControlServer>();
Workstation.Current.EnsureUpdateWorkspaceInfoCache(tfServer, tfServer.AuthorizedUser);

Have a look at this similar question: Microsoft.TeamFoundation.VersionControl.Client.ItemNotMappedException even when the workspace exists and has a mapping

like image 181
PatrickLu-MSFT Avatar answered Nov 07 '22 15:11

PatrickLu-MSFT