The scope of a sandboxed solution is site collection and site
collection administrator can activate / deactivate it. There may be some cases
where the farm administrator needs to have a check before activating the
solution. For such purpose Sandbox Solution Validator is used. In this episode
of Sandbox solution, we will look at HOW TO IMPLEMENT SANDBOX SOLUTION
VALIDATOR. Although it’s a feature which is not used often, I am still taking
it up as I know how handy it can be proved when you really need to implement a
rule for solution activation.
To create a solution validator you need to follow below steps
1.
Create an empty SharePoint Farm solution in VS
2.
Add a class SolutionValidator
in class file SolutionValidator.cs
3.
Inherit it from
SPSolutionValidator
and give a GUID to the class
[Guid("DAC77CB7-7511-4E7E-8427-B6A57C5F49F7")]
public class SolutionValidator : SPSolutionValidator
{
}
4.
Give name to the validator by defining a property in the validator
class
private
const string validatorName
= "Custom Solution Validator";
5.
Add constructors
public
SolutionValidator() { }
public
SolutionValidator(SPUserCodeService
userCodeService) : base(validatorName,
userCodeService)
{
this.Signature = 1111;
}
6.
Override ValidateSolution
method and set the properties.Valid
as true or false based on your logic. If properties.Valid
is false then SCA wouldn’t be able to activate the solution
public
override void
ValidateSolution(SPSolutionValidationProperties
properties)
{
base.ValidateSolution(properties);
// Write some validation logic here an
//properties.Valid = true;
}
7.
Override ValidateAssembly
method and set the properties.Valid
as true or false based on your logic. If properties.Valid
is false then SCA wouldn’t be able to activate the solution
public
override void
ValidateAssembly(SPSolutionValidationProperties
properties, SPSolutionFile assembly)
{
// Write some validation
logic here an
//properties.Valid = true;
}
Is it done? The answer is yes and
no. The validator is ready but it is not registered yet. To register the
validator to SPUC service, add following code in FeatureActivated method of the
feature in which this class is added
SPUserCodeService
sandboxService = SPUserCodeService.Local;
SPSolutionValidator
solutionValidator =
new
SolutionValidator(sandboxService);
sandboxService.SolutionValidators.Add(solutionValidator);
To
unregister the validator add following code in FeatureDeactivating method
SPUserCodeService
sandboxService = SPUserCodeService.Local;
Guid
solutionValidatorId = sandboxService.SolutionValidators["Custom Solution Validator"].Id;
sandboxService.SolutionValidators.Remove(solutionValidatorId);
Now deploy
the solution and try to activate any sandbox solution in solution gallery. You
should be able to see the rules/ logic, you wrote in validator, governing the solution
activation. Enjoy J
Please Note that I posted this blog orignally on http://aspe-it.com/blog/2013/sharepoint-2010-sandbox-solution-4-solution-validation/
No comments:
Post a Comment