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
Create a class library project.
Add references to the following assemblies:
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
Create a class that implements one of the following interfaces:
To create a package validation rule, implement the IPackageValidationRule interface.
To create a Feature validation rule, implement the IFeatureValidationRule interface.
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.
Example
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
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.");
}
}
}
}
Compiling the Code
This example requires references to the following assemblies:
Microsoft.VisualStudio.SharePoint.
System.ComponentModel.Composition.
Deploying the Extension
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.