Get active project - Extending Visual Studio SharePoint development tools tip #1

One of the things that I very often needed, while creating Visual Studio SharePoint development tools extensions, was a reference to the current project. No matter whether you want to add a reference or a new project item, a reference to the object that represents the current project is the first part of the solution.

Needed when

  • Adding a reference to the project
  • Adding a new Project Item


In your project you need the following assembly references:

  • EnvDTE (EmbedInteropTypes: True)
  • Microsoft.VisualStudio.OLE.Interop
  • Microsoft.VisualStudio.Shell.10.0
  • Microsoft.VisualStudio.Shell.Interop
  • Microsoft.VisualStudio.Shell.Interop.10.0 (EmbedInteropTypes: True)
  • Microsoft.VisualStudio.Shell.Interop.9.0
  • Microsoft.VisualStudio.Shell.Interop.8.0

You can also paste the following snippet into your project file:

<Reference Include="EnvDTE, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Reference Include="Microsoft.VisualStudio.OLE.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<Reference Include="Microsoft.VisualStudio.Shell.10.0, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop.10.0, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Reference Include="Microsoft.VisualStudio.Shell.Interop.9.0, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop.8.0, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />


internal static Project GetActiveProject()
    DTE dte = Package.GetGlobalService(typeof(SDTE)) as DTE;
    return GetActiveProject(dte);

internal static Project GetActiveProject(DTE dte)
    Project activeProject = null;

    Array activeSolutionProjects = dte.ActiveSolutionProjects as Array;
    if (activeSolutionProjects != null && activeSolutionProjects.Length > 0)
        activeProject = activeSolutionProjects.GetValue(0) as Project;

    return activeProject;


So far I wasn’t able to find any kind of API available as a part of the Visual Studio SharePoint development tools that would allow you to retrieve the current project. That’s why in order to get a reference to the Project object instance you need to fallback to the Visual Studio extensibility API.

The EnvDTE.Project interface doesn’t contain any SharePoint Project specific information. If you need to retrieve some information about your SharePoint Project you should use the ISharePointProject interface instead which is a part of the Visual Studio SharePoint development tools API and can be easily obtained from within your extension.

Technorati Tags: SharePoint 2010,Visual Studio 2010