Build a custom content provider for Sitecore Cortex Content Tagging
By default, the Sitecore CortexTM 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.
-
Use the configuration file to include or exclude fields from the tagging process based on field type or field name.
-
Use a custom content provider to exclude items from the tagging process.
This topic describes how to:
Implement a custom content provider
The content provider for Sitecore Cortex 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:
-
Implement the generic
Sitecore.ContentTagging.Core.Providers.IContentProvider
interface. -
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. -
Implement the content provider as illustrated in the following example:
RequestResponsec#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
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>
Enable a custom content provider
To enable a custom content provider:
-
Implement the custom configuration pipeline processor, as in the following example:
RequestResponseshellpublic class GetCustomConfigurationName { public void Process(GetTaggingConfigurationArgs args) { //perform check if () { args.ConfigurationName = "CustomConfiguration"; } } ...
-
In the configuration file, create a new named configuration set and include the new content provider, as in the following example:
RequestResponseshell<contentTagging> <configurations> <config name="CustomConfiguration"> <content> <provider name="CustomContentProvider"/> </content> <tagger> <provider name="DefaultTagger"/> </tagger> <taxonomy> <provider name="DefaultTaxonomyProvider"/> </taxonomy> </config> </configurations> </contentTagging>
-
Add the custom pipeline processor to the
getTaggingConfiguration
pipeline. With this processor, you can change the active configuration for the specific item type.RequestResponseshell<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>