The Proper Web Part Control Class for a SharePoint 2010 Sandbox Solution
This post is one post in a series of posts I penned in hopes it would help those looking to move a SharePoint 2010 Visual Studio 2010 farm based solution to a sandbox. There are many gotcha’s and pitfalls that you may run into or you may want to be aware of. You can learn more about this series here as well as download the CodePlex Project, From the Farm to the Sandbox, which includes the sample farm and sandbox solutions references through this series.
The related posts include:
Automatically Check In Files in a SharePoint 2010 Sandbox Feature Event Receiver
SharePoint 2010 Sandbox Feature Deactivation – Removing Module Assets
Custom Properties in a SharePoint 2010 Sandbox Web Part
Why CSSRegistration Will Not Work in a SharePoint 2010 Sandbox Solution
Different Types of Webparts
When you create a web part in a SharePoint project that was original built as for a farm solution, Visual Studio will assume that your web part will inherit from Microsoft.SharePoint.WebPartPages.WebPart. This is great because then you have access to more specific SharePoint web part properties and functions such as overriding the web part’s GetToolParts() function (public override ToolPart GetToolParts()) so that you can build your own ToolParts (Microsoft.SharePoint.WebPartPages.ToolPart).
SharePoint sandbox solutions do not have access to Microsoft.SharePoint.WebPartPages.WebPart nor Microsoft.SharePoint.WebPartPages.ToolPart, so you instead need to make sure that your web part inherits from System.Web.UI.WebControls.WebParts.WebPart. In fact it is now considered best practices to always have your web parts inherit from System.Web.UI.WebControls.WebParts.WebPart so that you can move it to SharePoint online, or to other environments.
So the key is, a web part for a SharePoint Sandbox must inherit from System.Web.UI.WebControls.WebParts.WebPart. In general this should not be a problem. I will describe how to replace a custom ToolPart control in the next post.
Visual Web Parts and Sandboxes
There is no OOTB Visual Web Part that is compatible with a sandbox because part of the visual aspects are required to be installed to the server hive. But not to worry, there are some great work-arounds. I suggest the following article from MSDN.
For a complete review of the Farm and Sandbox solutions’ Feature Event Receiver, please reference From the Farm to the Sandbox for the full code. Also, please do not forget to check out the other posts in this series, all related to converting a farm solution to a sandbox solution.