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.

Imports Microsoft.VisualStudio.SharePoint
Imports Microsoft.VisualStudio.SharePoint.Validation
Imports System.ComponentModel.Composition

<Export(GetType(IFeatureValidationRule))> _
Public Class CustomFeatureValidationRule
    Implements IFeatureValidationRule

    Public Sub ValidateFeature(ByVal context As IFeatureValidationContext) _
        Implements IFeatureValidationRule.ValidateFeature
        For Each projectItem In context.Feature.ProjectItems
            ValidateProjectItem(context, projectItem)
        Next projectItem
    End Sub 

    Public Sub ValidateProjectItem(ByVal context As IFeatureValidationContext, _
        ByVal projectItem As ISharePointProjectItem) _
        Implements IFeatureValidationRule.ValidateProjectItem
        If projectItem.Name = "" Then
            context.RuleViolations.Add( _
                "CustomFeatureValidationRule001", _
                ValidationRuleViolationSeverity.Warning, _
                "SharePoint project items must have a name.")
        End If 
    End Sub 
End Class

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