Build a custom content provider for Sitecore Content Tagging

Abstract

How to create a custom content provider for content tagging and how to add it to your environment.

By default, the Sitecore Content Tagging feature can process any content item in Sitecore. When it processes an item for tagging with the default configuration, it only includes the content of Multi-Line Text fields, Rich Text fields, and fields with the name Title. However, you can determine which fields are included in the tagging process. You can do this by changing the configuration file, or by using a custom content provider.

This topic describes how to:

The content provider for Sitecore Content Tagging returns content ready for analysis based on the object that the user selected for tagging.

As input, the content provider takes objects of type T, where T is a generic type.  For use with Sitecore, you will probably use the Item type for T.

As output, the content provider returns a TaggableContent object.

To create your own content provider:

  1. Implement the generic Sitecore.ContentTagging.Core.Providers.IContentProvider interface. 

  2. Specify the generic TSource parameter, which is an input parameter for a method that you must implement. In the default scenario, the input will be an item.

  3. Implement the content provider as illustrated in the following example:

    public class ExampleContentProvider: IContentProvider<Item>
    {
        public TaggableContent GetContent(Item source)
        {
            // TaggableContent is an abstract class and has no properties
            // that's why we use StringContent here
            var stringContent = new StringContent();
            // fetch content from your source object here
            stringContent.Content = source.Fields["Content"].Value;
            return stringContent;
        }
    }
    

Add the content provider to the configuration file, as in the following example:

<contentTagging>
    <providers>
        <content>
            <add name="ExampleContentProvider"
type="Custom.ContentTagging.Core.Providers.ExampleContentProvider,
Custom.ContentTagging.Core" />
        </content>
    </providers>
</contentTagging>

To enable a custom content provider:

  1. Implement the custom configuration pipeline processor, as in the following example:

    public class GetCustomConfigurationName
    {
        public void Process(GetTaggingConfigurationArgs args)
        {
            //perform check
            if ()
            {
                 args.ConfigurationName = "CustomConfiguration";
            }
         }
    ...
    
  2. In the configuration file, create a new named configuration set and include the new content provider, as in the following example:

    <contentTagging>
      <configurations>
        <config name="CustomConfiguration">
          <content>
            <provider name="CustomContentProvider"/>
          </content>
          <tagger>
            <provider name="DefaultTagger"/>
          </tagger>
          <taxonomy>
            <provider name="DefaultTaxonomyProvider"/>
          </taxonomy>
        </config>
      </configurations>
    </contentTagging> 
  3. Add the custom pipeline processor to the getTaggingConfiguration pipeline. With this processor, you can change the active configuration for the specific item type.

    <getTaggingConfiguration>
        <processor type="Sitecore.ContentTagging.Pipelines.GetTaggingConfiguration.GetDefaultConfigurationName, Sitecore.ContentTagging" resolve="true" />
        <processor type="Sitecore.ContentTagging.Pipelines.GetTaggingConfiguration.GetCustomConfigurationName, Sitecore.ContentTagging" resolve="true" />
        <processor type="Sitecore.ContentTagging.Pipelines.GetTaggingConfiguration.BuildConfiguration, Sitecore.ContentTagging" resolve="true" />
    </getTaggingConfiguration>