Build a custom tagger for Sitecore Content Tagging

Abstract

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

The tagger for Sitecore Content Tagging is responsible for tagging generic type T items. The tagger takes an item of type T and a collection of Tags objects as input, and then it assigns tags to the item.

This topic describes how to:

You must implement the generic T parameter, which is an object that you are going to tag, and an input parameter for a method. In the default scenario, T will be an Item.

To create a custom tagger:

  • Implement the generic Sitecore.ContentTagging.Core.Providers.ITagger interface as in the following example:

    public class ExampleTagger : ITagger<Item>
    {
        public void TagContent(Item contentItem, IEnumerable<Tag> tags)
        {
            // assign tags to your item here
            var semanticField = (MultilistField)contentItem.Fields["__Semantics"];
            contentItem.Editing.BeginEdit();
            foreach (var tag in tags)
            {
                if (ID.TryParse(tag.ID, out ID id) && !semanticField.TargetIDs.Contains(id))
                {
                    semanticField.Add(tag.ID);
                }
            }
            contentItem.Editing.EndEdit();
        }
    }

To use your provider in a content tagging configuration:

  • Register the provider in the configuration file, as in the following example:

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