HtmlValidationContext.ValidateImageValue Method

Removes unsafe or disallowed URLs and HTML markup and returns a validated imageValue parameter.

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

public ImageFieldValue ValidateImageValue(
	ImageFieldValue imageValue,
	out bool tagsWereDropped,
	out bool urlsWereDropped


Type: Microsoft.SharePoint.Publishing.Fields.ImageFieldValue

The ImageFieldValue object that is validated.

Type: System.Boolean

On return, this parameter is set to True if any disallowed tags were found and stripped during validation, otherwise False.

Type: System.Boolean

On return, this parameter is set to True if any disallowed URLs were found and stripped during validation, otherwise False.

Return Value

Type: Microsoft.SharePoint.Publishing.Fields.ImageFieldValue
The validated image value, from which all unsafe or disallowed content is removed.

Removes any tags, attributes, or markup from an ImageFieldValue object that are unsafe or do not conform to the constraint settings that are applied to this HtmlValidationContext object.

The following properties do not affect this method: AllowFonts, AllowHeadings, AllowLists, AllowReusableContent, AllowTables, and AllowTextMarkup.

The AllowImages property is always forced to be set to True.

The AllowHyperlinks and RestrictUrlsToSiteCollection properties can be set to either True or False.

This sample constructs an HtmlValidationContext object and uses it to restrict and validate an ImageFieldValue object and return a report string. The sample functiontakes in two optional arguments:

  • imageValue: An ImageFieldValue object to run through the validation. If this argument is empty then the application uses a default ImageFieldValue object instead.

  • siteCollectionToRestrict: An SPSite to use for restricting the URLs in the HTML. If this argument is null then the URLs are not restricted to an SPSite.

// ValidateImageValueSample
using ImageFieldValue = Microsoft.SharePoint.Publishing.Fields.ImageFieldValue;
using LinkFieldValue = Microsoft.SharePoint.Publishing.Fields.LinkFieldValue;
using HtmlValidationContext = Microsoft.SharePoint.Publishing.Fields.HtmlValidationContext;
namespace Microsoft.SDK.SharePointServer.Samples

        public static string ValidateImageValueSample(
            ImageFieldValue imageValue,
            SPSite siteCollectionToRestrict)
            ImageFieldValue imageValueToValidate = imageValue;
                    // If there is no provided value then construct a new default value
            if (null == imageValueToValidate)
                imageValueToValidate = new ImageFieldValue();
                imageValueToValidate.ImageUrl = DefaultImageUrl;
                imageValueToValidate.Hyperlink = DefaultHyperlink;
            string reportString = "Validating the following image value \n[" + imageValueToValidate.ToString() + "]";

            HtmlValidationContext validationContext = new HtmlValidationContext();

            // Set the constraint properties that apply for image values
            validationContext.AllowHyperlinks = true;

            if (null == siteCollectionToRestrict)
                // No site collection provided so do not restrict URLs
                validationContext.RestrictUrlsToSiteCollection = false;
                validationContext.GuidOfThisSiteCollection = System.Guid.Empty;
                // Restrict URLs to be from the provided site collection or to be server relative
                validationContext.RestrictUrlsToSiteCollection = true;
                validationContext.GuidOfThisSiteCollection = siteCollectionToRestrict.ID;

            bool droppedTags;
            bool droppedUrls;
            ImageFieldValue validatedValue = 
                    out droppedTags,
                    out droppedUrls);

            reportString += "After validation we have the following image value \n[" + validatedValue.ToString() + "]";

            return reportString;