Sunday, 18 March 2012

MP.SPDevExt VSIX for SharePoint 2010: Sandbox Solution Validator

MP.SPDevExt Blog Series:
Part 1: MP.SPDevExt VSIX for SharePoint 2010
Part 2: MP.SPDevExt VSIX for SharePoint 2010 : Custom Field Type
Part 3: MP.SPDevExt VSIX for SharePoint 2010 : Sandbox Workflow Action (Coming soon)
Part 4: MP.SPDevExt VSIX for SharePoint 2010 : Sandbox Solution Validator (Currently reading)


You know what I like….keeping toys on my desk…..

You know what I don’t like….if somebody plays with them…especially with my favorite Red car.

Everybody by now must be aware of the boom that Sandbox solutions has created after SharePoint 2010 launch. I will blog about Sandbox solutions in detail separately. Let us stick to Sandbox Solution Validator functionality provided by SharePoint 2010.

Sandbox Solution Validators (in brief)
These are used to apply the validations when Sandbox solutions are activated.

Why they are needed?
Sandbox solutions are uploaded and activated by Site Collection Administrators. Farm Administrator does not come into picture here… There could be some validations/ checks that he/she wants every Site Collection Administrator to follow for all the Sandbox Solutions throughout the farm. But Site Collection Administrators can ignore the checks or they can activate the invalid solution mistakenly. 

So address this, SharePoint 2010 has given a provision for Farm Administrator to create and apply the Solution Validators. Once applied SharePoint won't allow activation of the Sandbox solution not passing the validation logic mentioned in the Solution Validators.

Once both Sandbox architecture and Sandbox solution validators are in action….farm administrators can happily take a nap or watch a cricket match without worrying about the health of their farm.

Let us see step by step how to bring the Sandbox Solution Validators in action using Sandbox Solution Validator item template present in MP.SPDevExt.

1.       Open the MP.SPDevExtDemo project created in the part 1. 
2.       Go to Solution Explorer and right click on MP.SPDevExtDemo Project and click on Add=>New Item

Figure 1 : Add New Item

3.       In Add New Item window, select the SharePoint=>2010 tab in left menu. Then click on Sandbox Solution Validator (MP.SpDevExt) item template, type SolutionNameValidator in Name textbox and click on Add button.

Figure 2: Add New Sandbox Solution Validator

4.       This will create the SolutionNameValidator project item as shown if figure 3

Figure 3: SolutionNameValidator project item

The below table shows the files generated by Sandbox Solution Validator item template along with their usages. 

Files
Usage
SolutionNameValidator.cs
The contains a class inherited from SPSolutionValidator class.
This class provides methods to validate the solution (ValidateSolution) and  assemblies within (ValidateAssembly)
SolutionNameValidatorFeatureReceiver.cs
This is feature receiver which contains code to register the solution validator with User Code service.
Feature1
A farm scoped feature with above mentioned feature receiver attached


5.       By default, Sandbox Solution Validator item template generates the code which allows the Sandbox solutions to be activated only if their name starts with “mp.spdevext.” We will keep this default logic as it is.
6.       Right click in MP.SPDevExtDemo project and click on Deploy menu as shown in figure 5. Visual studio will package and deploy the solution to the farm.

Figure 4: Deploy Solution

7.       After successful deployment, upload any sandbox solution to the solution store not having name starting with “mp.spdevext.”. Then try activating the solution. It will give an ugly yellow error message shown in figure 5.

Figure 5: Validation Error Message


You can customize the validation logic ValidateSolution and ValidateAssembly methods.

Go ahead and use…Please share your feedback/ suggestion.

No comments:

Post a Comment