Share via


HtmlValidationContext class

This class handles all validation checking and HTML filtering, based on constraint properties that are set on the validation context.

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.Publishing.Fields.HtmlValidationContext

Namespace:  Microsoft.SharePoint.Publishing.Fields
Assembly:  Microsoft.SharePoint.Publishing (in Microsoft.SharePoint.Publishing.dll)

Syntax

'Declaration
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Class HtmlValidationContext
'Usage
Dim instance As HtmlValidationContext
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class HtmlValidationContext

Remarks

The caller sets various constraints by setting the different constraint properties first. Then, the caller can invoke the ValidateHtml method to validate the input HTML and get the filtered HTML in return. Any input HTML markup that is determined to be unsafe or invalid, such as script and object tags, is also always removed from the content regardless of constraint property settings.

Use this class for validation in the RichHtmlField control to validate and provide feedback to the user about failures.

Examples

The ValidateHtmlCode sample constructs an HtmlValidationContext object with various settings and uses it to restrict and validate HTML content and return a report string. The sample function takes two optional arguments:

  • htmlContent. A string of HTML to run through validation. If this string is empty, then the application uses a default string of test HTML.

  • siteCollectionToRestrict. A SPSite object used to restrict the URLs present in the HTML. If this is set to null then the URLs are not restricted to an SPSite object.

    using SPSite = Microsoft.SharePoint.SPSite;
    using HtmlValidationContext = Microsoft.SharePoint.Publishing.Fields.HtmlValidationContext;
    
    namespace Microsoft.SDK.SharePointServer.Samples
    {
        public static class HtmlValidationContextSamples
    
    public static string ValidateHtmlSample(
      string htmlContent,
      SPSite siteCollectionToRestrict)
    {
      string htmlContentToValidate = htmlContent;
      if (string.IsNullOrEmpty(htmlContentToValidate))
      {
         htmlContentToValidate = DefaultHtmlToValidate;
      }
    
    string reportString = "Validating the following HTML \n[" + htmlContentToValidate + "]";
    
    HtmlValidationContext validationContext = new HtmlValidationContext();
    
    reportString += 
    "First validate with no constraints to remove unsafe content only \n[" + 
    validationContext.ValidateHtml(htmlContentToValidate) + "]";
        {
            // You can change the following default HTML to validate
            private const string DefaultHtmlToValidate = @"