Get active project – Extending Visual Studio SharePoint development tools tip #1
SharePoint 2010, Tips & Tricks, Visual Studio 2010One 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.

















October 3rd, 2011 at 12:42 pm
That is what i needed, Thanks Waldek
October 5th, 2011 at 7:29 am
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.
October 10th, 2011 at 4:10 am
@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.
October 17th, 2011 at 2:16 pm
@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
October 17th, 2011 at 7:42 pm
@Anand: Which version of Visual Studio are you using? Did you reference correct DLLs?
October 18th, 2011 at 8:12 am
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
January 5th, 2012 at 8:51 pm
Did you ever find a solution to the GetGlobalService returning null problem? I'm seeing the same thing.