Thursday, July 12, 2012

Hacking the Visual Web Part project template in Visual Studio 2010

If you have worked with the Visual Web Part project in Visual Studio 2010, you already know that you cannot use a Sandboxed-compatible Visual Web Part project.  Instead, you have to use the Item Template that ships with the SharePoint Power Tools to achieve this solution.

However, if you look at the underlying solution that Microsoft has provided with the use of the SharePoint Power Tools, it is really just a sleight of hand that can easily be worked around by manually "hacking" the original Visual Web Part project template.

If you look at the Project Template (.vstemplate file) for a Visual Web Part project, you will find the following lines:
<WizardExtension>
    <Assembly>Microsoft.VisualStudio.SharePoint.ProjectExtensions.Wizards, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
    <FullClassName>Microsoft.VisualStudio.SharePoint.ProjectExtensions.Wizards.WebPartProjectWizard</FullClassName>
  </WizardExtension>

However, if you look at the Project Template (.vstemplate file) for an Empty SharePoint Project, you will find the following lines:
  <WizardExtension>
    <Assembly>Microsoft.VisualStudio.SharePoint.ProjectExtensions.Wizards, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
    <FullClassName>Microsoft.VisualStudio.SharePoint.ProjectExtensions.Wizards.EmptyProjectWizard</FullClassName>
  </WizardExtension>

Interestingly enough, the Wizard Extension for an Empty SharePoint project provides the capability to create a sandboxed-compatible solution as well as a Farm solution.  However, when you create a Visual Web Part project, you can only create Farm Level solutions.

But, the instructions for creating a sandboxed-compatible Visual Web Part is to first create an Empty SharePoint project and then to add a Visual Web Part (Sandboxed) Item Template.

So it just looks like Microsoft is really leveraging the sandboxed capabilities from the Empty SharePoint project template!

In any case, I started playing around with manipulating and modifying the Project Templates and sure enough, I was able to inject the WizardExtension for an Empty SharePoint project into a Project Template for a Visual Web Part project.

After doing this and importing the new SharePoint Project Template into Visual Studio 2010, I was able to use a Visual Studio 2010 Web Part project template interchangeably for sandboxed solutions as well as Farm solutions!

No comments:

Post a Comment