TFS API: GetLocalWorkspaceInfo always returns null

After migrating from TFS2013 to TFS2017 in the company I work for I had the same problem with Workstation.Current.GetLocalWorkspaceInfo.

What worked for me is a call to Workstation.EnsureUpdateWorkspaceInfoCache:

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

I added the above code lines to the constructor of my TFS proxy class that uses GetLocalWorkspaceInfo.


I know this is an old post, but just like to share the workaround that we have, by using VersionControlServer.QueryWorkspaces to query all the workspaces for the user on his/her machine.

private static Workspace FindWorkspaceByPath(TfsTeamProjectCollection tfs, string workspacePath)
{ 
    VersionControlServer versionControl = tfs.GetService<VersionControlServer>();

    WorkspaceInfo workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(workspacePath);

    if (workspaceInfo != null)
    {
        return versionControl.GetWorkspace(workspaceInfo);
    }

    // No Workspace found using method 1, try to query all workspaces the user has on this machine.
    Workspace[] workspaces = versionControl.QueryWorkspaces(null, Environment.UserName, Environment.MachineName);
    foreach (Workspace w in workspaces)
    {
        foreach (WorkingFolder f in w.Folders)
        {
            if (f.LocalItem.Equals(workspacePath))
            {
                return w;
            }
        }
    }

    throw new Exception(String.Format("TFS Workspace cannot be determined for {0}.", workspacePath));
}

When executing tf workspaces (on my computer) in the Visual Studio 2010 command prompt it says No workspace matching * found on this computer, but when executing the same command in Visual Studio 2012 it returns back all my expected workspaces.

The issue can be resolved by doing any of the following:

  • Reference the version of the Microsoft.TeamFoundation.VersionControl.Client dll that was connected with Visual Studio 2012 instead of the dll connected with Visual Studio 2010.

  • Open Visual Studio 2010 and connect it to TFS to where it will create the workspaces for Visual Studio 2010

Tags:

C#

Tfs Sdk