Search code examples
visual-studiotfsenvdtevisual-studio-sdk

How to work out TFS uri and location for currently loaded solution projects in Visual Studio Extension


I am trying to work out how, in my Visual Studio Extension, I can work out which of the currently loaded projects are in TFS and then the TFS server uri and location for each of these projects.

I would like to be able to do something like this:

foreach (EnvDTE.Project project in dte.Solution.Projects)
{
    if (project.IsInTFS) {   
        var uri = project.TFSUri;             // http://tfsserver:8080/tfs
        var location = project.TFSLocation;   // $\TeamProject\Project
    }
}

Can anyone help me?

Thank you.


Solution

  • You can use the SourceControl2 interface to accomplish this in the following way:

    foreach (Project project in dte.Solution.Projects)
    {
        if (dte.SourceControl.IsItemUnderSCC(project.FileName))
        {
            var bindings = project.GetSourceControlBindings();
            var uri = bindings.ServerName;             
            var location = bindings.ServerBinding;
            // your logic goes here
        }
    }
    

    Where GetSourceControlBindings is an extension method:

    public static SourceControlBindings GetSourceControlBindings(this Project project)
    {
        SourceControl2 sourceControl = (SourceControl2)project.DTE.SourceControl;
        return sourceControl.GetBindings(project.FullName);
    }
    

    Note: you will have to a add reference to the EnvDTE80.dll assembly.


    If you're interested exclusively in TFS source control, then you should add one more condition:

    if (bindings.ProviderName == "{4CA58AB2-18FA-4F8D-95D4-32DDF27D184C}")
    

    Where that magic guid specifies the Team Foundation Source Control Provider.