How to: Create Custom Feature and Package Validation Rules for SharePoint Solutions

You can create custom validation rules to verify the solution package generated by Visual Studio. You can perform full validation on an entire Feature or package by selecting Validate from the context menu of a package or Feature in the Packaging Explorer. Partial validation is performed when you add new SharePonit project items or Features to the project to determine if the Package or Feature would be in a valid state.

To create a custom package validation rule

  1. Create a class library project.

  2. Add references to the following assemblies:

    • Microsoft.VisualStudio.SharePoint

    • System.ComponentModel.Composition

  3. Create a class that implements one of the following interfaces:

  4. Add the System.ComponentModel.Composition.ExportAttribute to the class. This attribute enables Visual Studio to discover and load your validation rule. Pass the IPackageValidationRule or IFeatureValidationRule type to the attribute constructor.

The following code example demonstrates how to create a custom Feature validation rule.

using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Validation;
using System.ComponentModel.Composition;

namespace Extension
{
    [Export(typeof(IFeatureValidationRule))]
    internal class CustomFeatureValidationRule : IFeatureValidationRule
    {
        public void ValidateFeature(IFeatureValidationContext context)
        {
            foreach (var projectItem in context.Feature.ProjectItems)
            {
                ValidateProjectItem(context, projectItem);
            }
        }

        public void ValidateProjectItem(
            IFeatureValidationContext context,
            ISharePointProjectItem projectItem)
        {
            if (projectItem.Name == "")
            {
                context.RuleViolations.Add(
                    "CustomFeatureValidationRule001",
                    ValidationRuleViolationSeverity.Warning,
                    "SharePoint project items must have a name.");
            }
        }
    }
}

This example requires references to the following assemblies:

  • Microsoft.VisualStudio.SharePoint.

  • System.ComponentModel.Composition.

To deploy the extension, create a Visual Studio extension (VSIX) package for the assembly and any other files that you want to distribute with the extension. For more information, see Deploying Extensions for the SharePoint Tools in Visual Studio.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft