SharePoint 2010 Sandboxed Solutions Solution Validator ( SPSolutionValidator )

Title: Sandboxed Solutions Solution Validator ( SPSolutionValidator )

Details: Now that you already know how to create a Sandboxed Solution, in a typical scenario Farm Admins need some one or some mechanism to evaluate or validate the solutions developed using Sandboxed solutions by the developers. Here is the example of how and what you need to inherit and extend the ootb available classes to write your own.

Code: First, here are the namespaces required

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

using System.Runtime.InteropServices;
using System.Collections.ObjectModel;

using Microsoft.SharePoint.UserCode; // Under > C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\UserCode\assemblies\

You then need a class that inherits from SPSolutionValidator and overrides couple of its methods as show below. Remmember that this code is going to fire for every other solution thats is added to the solution gallery on a Site Collection.
namespace AltsisSolutionValidator
{
    [Guid("481823F5-75A7-4EF8-8A4B-11C4D52D1014")]
    public class SolutionValidator : SPSolutionValidator
    {
        private const string strValidatorName = "Altsis Validator Name";
        
        public SolutionValidator()
        { 
        
        }

        public SolutionValidator(SPUserCodeService userCodeService)  : base(strValidatorName, userCodeService)
        {
            this.Signature = 1977;
        }

        public override void ValidateAssembly(SPSolutionValidationProperties properties, SPSolutionFile assembly)
        {
            base.ValidateAssembly(properties, assembly);
            properties.Valid = true;
        }

        public override void ValidateSolution(SPSolutionValidationProperties properties)
        {
            base.ValidateSolution(properties);
            ReadOnlyCollection files = properties.Files;

            // Write your validations etc here- extend it as much as you can         
            
            properties.ValidationErrorMessage = " Solution Suceess / Failed ";
         }
    }
}

Alright, now you need a feature to deploy this to Farm Level.
namespace AltsisSolutionValidator.Features.AltsisSolutionValidatorFeature
{
    /// 
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// 
    /// 
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// 

    [Guid("2a4d3a6b-ab9c-4008-9408-26dd4cd1f6d8")]
    public class AltsisSolutionValidatorFeatureEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPUserCodeService.Local.SolutionValidators.Add(new SolutionValidator(SPUserCodeService.Local));
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPUserCodeService.Local.SolutionValidators.Remove(new Guid("481823F5-75A7-4EF8-8A4B-11C4D52D1014"));
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary parameters)
        {
        }
    }
}

Here is my codeplex project template that you can download
http://spsolutionvalidator.codeplex.com/

Thats it. Fire away and see the results for yourselves.

Comments