Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How programmatically check is project mapped in TFS?

Tags:

c#

tfs

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 
    }
like image 574
Дмитро Кононенко Avatar asked Oct 21 '22 05:10

Дмитро Кононенко


1 Answers

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);
     }
  }
} 
like image 103
MikeR Avatar answered Oct 31 '22 21:10

MikeR