Farm Validators validate Sandboxed Solutions when they are uploaded to the Solution Gallery. Validators are called when solutions are activated and updated validators will be called when solutions will be re-validated in the next execution.
Here are the steps to create a Solution Validator.
1. Create an Empty SharePoint project
2. Add a class called Validator.cs
3. Copy and paste the following code:
using System; using System.Runtime.InteropServices; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint.UserCode; using Microsoft.SharePoint.Administration; namespace SolutionValidator { public struct Constants { public const string ValidatorGuid = "99A53731-6DCF-42AF-9220-C4C45638B570"; } [Guid(Constants.ValidatorGuid)] public class Validator : SPSolutionValidator { const string _name = "My Validator"; // change this to update the validator const int _signature = 100; public Validator() {} public Validator(SPUserCodeService ucService) : base(_name, ucService) { this.Signature = _signature; } /// <summary> /// Validate the solution /// </summary> /// <param name="properties"></param> public override void ValidateSolution(SPSolutionValidationProperties properties) { base.ValidateSolution(properties); properties.Valid = ValidationLogic(); properties.ValidationErrorMessage = "Solution is invalid"; properties.ValidationErrorUrl = "/_layouts/ValidationErrorPage.aspx"; } bool ValidationLogic() { // Put any validation logic here return true; } /// <summary> /// Validate the assembly /// </summary> /// <param name="properties"></param> /// <param name="assembly"></param> public override void ValidateAssembly(SPSolutionValidationProperties properties, SPSolutionFile assembly) { base.ValidateAssembly(properties, assembly); properties.Valid = ValidationLogic(); } } }
4. Add a feature and add a event handler. Add the following code to it:
using System; using System.Runtime.InteropServices; using System.Security.Permissions; using Microsoft.SharePoint; using Microsoft.SharePoint.Security; using Microsoft.SharePoint.Administration; namespace SolutionValidator.Features.SolutionValidator { /// <summary> /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade. /// </summary> /// <remarks> /// The GUID attached to this class may be used during packaging and should not be modified. /// </remarks> [Guid("65fad458-1411-48ea-a98f-606de492bba8")] public class SolutionValidatorEventReceiver : SPFeatureReceiver { /// <summary> /// Adds the solution validator /// </summary> /// <param name="properties"></param> public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPUserCodeService.Local.SolutionValidators.Add(new Validator(SPUserCodeService.Local)); } /// <summary> /// Removes the solution validator /// </summary> /// <param name="properties"></param> public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { SPUserCodeService.Local.SolutionValidators.Remove(new Guid(Constants.ValidatorGuid)); } } }
Install and activate this feature. Now depending on your validation logic, you can control how sandboxed solutions are activated. Remember to change the signature field when you update the validator.
0 comments:
Post a Comment
I always welcome feedback from my readers.