Partilhar via


HtmlValidationContext.ValidateLinkValue Method

Strips out unsafe or disallowed URLs and HTML markup and returns a validated LinkFieldValue object.

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

Syntax

'Declaration
Public Function ValidateLinkValue ( _
    linkValue As LinkFieldValue, _
    <OutAttribute> ByRef tagsWereDropped As Boolean, _
    <OutAttribute> ByRef urlsWereDropped As Boolean _
) As LinkFieldValue
'Usage
Dim instance As HtmlValidationContext
Dim linkValue As LinkFieldValue
Dim tagsWereDropped As Boolean
Dim urlsWereDropped As Boolean
Dim returnValue As LinkFieldValue

returnValue = instance.ValidateLinkValue(linkValue, _
    tagsWereDropped, urlsWereDropped)
public LinkFieldValue ValidateLinkValue(
    LinkFieldValue linkValue,
    out bool tagsWereDropped,
    out bool urlsWereDropped
)

Parameters

  • tagsWereDropped
    Type: System.Boolean

    On return, this parameter is set to True if any disallowed tags are found and removed during validation. Otherwise, set to False.

  • urlsWereDropped
    Type: System.Boolean

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

Return Value

Type: Microsoft.SharePoint.Publishing.Fields.LinkFieldValue
The validated LinkFieldValue object from which all unsafe or disallowed content has been removed.

Remarks

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

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

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

Examples

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

  • linkValue. A LinkFieldValue object to run through the validation. If this argument is empty then the application uses a default LinkFieldValue object.

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

// ValidateLinkValueSample
using SPSite = Microsoft.SharePoint.SPSite;
using ImageFieldValue = Microsoft.SharePoint.Publishing.Fields.ImageFieldValue;
using LinkFieldValue = Microsoft.SharePoint.Publishing.Fields.LinkFieldValue;
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 ValidateLinkValueSample(
            LinkFieldValue linkValue,
            SPSite siteCollectionToRestrict)
        {
            LinkFieldValue linkValueToValidate = linkValue;

            // If there is no provided value then construct a new default value
            if (null == linkValueToValidate)
            {
                linkValueToValidate = new LinkFieldValue();
                linkValueToValidate.href = DefaultHyperlink;
                linkValueToValidate.Text = LinkFieldValue.GetDefaultDisplayText(DefaultHyperlink);
            }
            string reportString = "Validating the following link value \n[" + linkValueToValidate.ToString() + "]";

            HtmlValidationContext validationContext = new HtmlValidationContext();

            if (null == siteCollectionToRestrict)
            {
                // No site collection provided so do not restrict URLs
                validationContext.RestrictUrlsToSiteCollection = false;
                validationContext.GuidOfThisSiteCollection = System.Guid.Empty;
            }
            else
            {
                // 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;
            LinkFieldValue validatedValue = 
                validationContext.ValidateLinkValue(
                linkValueToValidate,
                out droppedTags,
                out droppedUrls);

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

            return reportString;
        }
    }
}

See Also

Reference

HtmlValidationContext Class

HtmlValidationContext Members

Microsoft.SharePoint.Publishing.Fields Namespace