I need to find out is project mapped locally or not from the code. I can get all TFS project using Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory.GetTeamProjectCollection()
, than I can do foreach
for workItemStore = new WorkItemStore(projects)
and get a lot of project information but anything like IsMapped
or MappingPath
.
Information I'm needed is easily accessed from Source Control Explorer of Team Explorer in Visual Studio, but I need to do it from C# code.
That is what I tried:
var projects = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(_tfsServerUri));
projects.Authenticate();
var workItemStore = new WorkItemStore(projects);
foreach (Project pr in workItemStore.Projects)
{
pr.IsLocal;
}
UPD: ANSWER
MikeR's answer is good, but I want to add that it has one defect. If you have your root directory mapped, but you don't actually have all projects from this root directory on your local computer, Miker's solution will still return all projects. If you don't want your code to act this way, here is my solution:
TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(_tfsServerUri));
teamProjectCollection.Authenticate();
VersionControlServer versionControl = teamProjectCollection.GetService<VersionControlServer>();
string computerName = Environment.MachineName;
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
// get yours local workspaces
Workspace[] workspaces = versionControl.QueryWorkspaces(null, windowsIdentity.Name, computerName);
foreach (Project pr in workItemStore.Projects)
{
var mapped = false;
foreach (Workspace workspace in workspaces)
{
var path = workspace.TryGetLocalItemForServerItem("$/" + pr.Name);
if (!String.IsNullOrEmpty(path) && Directory.Exists(path))
{
mapped = true;
}
}
// do what you want with mapped project
}
This is more a generic approach, but I think you will manage to customize it for your needs (not compiled, just pointing to the direction):
string project = "TeamProject1";
string serverPath = "$/"+project;
string computername = "myComputer"; // possibly Environment.Computer or something like that
var tpc= TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(_tfsServerUri));
tpc.Authenticate();
// connect to VersionControl
VersionControlServer sourceControl = (VersionControlServer)tpc.GetService(typeof(VersionControlServer));
// iterate the local workspaces
foreach (Workspace workspace in sourceControl.QueryWorkspaces(null, null, computername))
{
// check mapped folders
foreach (WorkingFolder folder in workspace.Folders)
{
// e.g. $/TeamProject1 contains $/ if the root is mapped to local
if (serverPath.Contains(folder.ServerItem) && !folder.IsCloaked)
{
Console.WriteLine(serverPath + " is mapped under "+ folder.LocalItem);
Console.WriteLine("Workspacename: "+workspace.Name);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With