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

References

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=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
  <EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Microsoft.VisualStudio.OLE.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<Reference Include="Microsoft.VisualStudio.Shell.10.0, Version=10.0.0.0, 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=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
  <EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Microsoft.VisualStudio.Shell.Interop.9.0, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop.8.0, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

Code

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;
}

Remarks

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.


Possibly related posts

7 Responses to “Get active project – Extending Visual Studio SharePoint development tools tip #1”

  1. Sandip Says:

    That is what i needed, Thanks Waldek

  2. Anand Says:

    Hi Mastykarz,

    I'm creating a VS2010 AddIn.
    I've a situation where I need to convert an EnvDTE project object to an IsharePointProject object in order to access ISharePointProject's property "IsSandBoxed".
    Kindly suggest a way or workaround so that I could access this "IsSandBoxed" Property.

  3. Waldek Mastykarz Says:

    @Anand: I suggest you check out CKS:DEV code on CodePlex. We have there a helper function that allows you to convert EnvDTE to ISharePointProject.

  4. Anand Says:

    @Waldek : Hey thanks a lot Waldek! It helped a lot, but I'm getting null value for dte when I use follwing line of code in mine

    DTE dte = Package.GetGlobalService(typeof(SDTE)) as DTE;

    I'm not able to figure out why?
    Kindly suggest.

    Thanks,
    Anand

  5. Waldek Mastykarz Says:

    @Anand: Which version of Visual Studio are you using? Did you reference correct DLLs?

  6. Anand Says:

    I'm using VS 2010.
    If the DLLs referenced would have been wrong, I'd have got build errors I believe, but the build works fine. But Package.GetGlobalService(typeof(SDTE))
    is returning null value

  7. Eric Gurney Says:

    Did you ever find a solution to the GetGlobalService returning null problem? I'm seeing the same thing.

Leave a Reply

Security Code:

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS
Copyright © 2007 - 2012 Waldek Mastykarz

Creative Commons License