How to get TFS server address from a local folder mapped to a workspace

Sometimes you need to operate to a Tfs Version Control System with API starting from a local folder. There are a lot of API to work with workspaces, but the main problem is that 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?

The exercise is the following one :) – Write a snippet of code, based on TFS API that, given a local path, retrieve ChangesetId that is currently mapped on the workspace.

The key part is the Workstation class that permits you to obtain a WorkspaceInfo object from a simple Path, thanks to the GetLocalWorkspaceInfo method.

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

Now that you have a workspace info, getting the info you need is really simple, first of all you can know the Team Project Collection address thanks to the ServerUri property of the WorkspaceInfo object, moreover the WorkspaceInfo object can create a valid Workspace object with a simple call to GetWorkspace method, passing a valid TfsTeamProjectCollection object. Here is the code.

1
2
3
4
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(info.ServerUri);
Workspace ws = info.GetWorkspace(collection);
String servername = ws.GetServerItemForLocalItem(path);
VersionControlServer vcs = collection.GetService<VersionControlServer>();

Now you have both the VersionControlServer object and the Workspace object, and you can use the QueryHistory method to know the information you need.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
IEnumerable changesets = null;
VersionSpec spec = VersionSpec.ParseSingleSpec("W", ws.OwnerName);
 
WorkspaceVersionSpec wvs = spec as WorkspaceVersionSpec;
if (null != wvs && null == wvs.Name)
{
spec = new WorkspaceVersionSpec(ws.Name, ws.OwnerName);
}
changesets = vcs.QueryHistory(servername, VersionSpec.Latest, 0, RecursionType.Full, null,
new ChangesetVersionSpec(1), spec, 1, false, false).OfType<Changeset>().ToList();
int changesetId = changesets.Cast<Changeset>().First().ChangesetId;

And you have the ChangesetId currently mapped to the local folder.

Alk