共用方式為


Create feature and package validations for SharePoint solutions

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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 PackagingExplorer. Partial validation is performed when you add new SharePoint 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 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.");
            }
        }
    }
}

Compile the code

This example requires references to the following assemblies:

  • Microsoft.VisualStudio.SharePoint.

  • System.ComponentModel.Composition.

Deploy 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 Deploy Extensions for the SharePoint tools in Visual Studio.

See also