Extending Sitecore CMP connector to synchronize images

Sitecore Content Hub CMP Connector Extensions

How to extend Sitecore’s Connect Hub CMP connector to synchronise images.

Versions used: Sitecore Experience Platform 9.2 Initial Release. Content Hub 3.2.0

This post is way overdue as I was supposed to release it after my last talk at The Sitecore Sessions. Sorry for the delay 😞.

On a previous post, I explained how to install Sitecore Connect™ for Sitecore CMP to automatically send entities from CMP to Sitecore. Alas, the connector only synchronises text fields.

This post explains the CMP connector extension that synchronises Content Hub images set on M.Content entities. Using the same methodology, this could grow into a module and include other field types (e.g. references to other entities). Source Code here.

This solution extends the existing CMP connector by adding a new processor to process a new mapping field type that maps an image relation to a Sitecore field using the same format supported by Sitecore connector for Content Hub DAM; so DAM connector needs to be installed.

The default connector will process items of template type /sitecore/templates/CMP/Field Mapping, so we need a new template item (/sitecore/templates/CMP/Image Field Mapping) that our custom processor can use to identify image mapping configurations. This template has Field Mapping as its base template plus 2 extra fields: AssetIndex to specify the index of the asset relation to grab (if multiple are allowed), and RenditionToUse to specify which asset rendition public link to use. CMP Field Name is used as the relation to find the asset on (CmpContentToLinkedAsset is available OOTB).

The new processor runs after the default ones.

1
2
3
4
5
<pipelines>          
<cmp.importEntity>
<processor type="Sitecore.SharedSource.CMP.Connector.CustomFields.Pipelines.SaveImageFieldValues, Sitecore.SharedSource.CMP.Connector.CustomFields" patch:after="processor[@type='Sitecore.Connector.CMP.Pipelines.ImportEntity.SaveFieldValues, Sitecore.Connector.CMP']"/>
</cmp.importEntity>
</pipelines>

This processor SaveImageFieldValues.cs fetches all Field Mapping items (from ImportEntityPipelineArgs object) under the Entity Mapping item being processed, reads required field values to find the rendition from the relation and stores the formatted public link URL into the specified sitecore field.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
foreach (Item item in from i in args.EntityMappingItem.Children
where i.TemplateID == Constants.ImageFieldMappingTypeID
select i)
{
var cmpFieldName = item[Sitecore.Connector.CMP.Constants.FieldMappingCmpFieldNameFieldId];
var sitecoreFieldName = item[Sitecore.Connector.CMP.Constants.FieldMappingSitecoreFieldNameFieldId];

var renditionField = item[Constants.FieldMappingSitecoreRenditionFieldID];
var publicLink = GetPublicLinkData(contentHubHost, args.Entity, cmpFieldName, renditionField, assetIndexField).GetAwaiter().GetResult();

if(publicLink != null && !string.IsNullOrEmpty(publicLink.URL))
{
var imgElement = GetContentHubDamImageElement(contentHubHost, publicLink);
args.Item[item[Sitecore.Connector.CMP.Constants.FieldMappingSitecoreFieldNameFieldId]] = imgElement;
}
}

As you can see, the code is quite simple. It loops through ImageFieldMapping items and reads the pertinent fields; gets or creates a public link to the defined rendition, and stores the formatted image tag into the image field value.

<image stylelabs-content-type="Image" mediaid="" src="https://HOST/api/public/content/364944895?v=779a" height="1100" alt="" stylelabs-content-id="1235" width="790" thumbnailsrc="https://HOST/api/gateway/1235/thumbnail" />

====================
References:
https://github.com/josedbaez/Sitecore.SharedSource.CMP.Connector.Extensions


Please let me know what you think and/or if you can spot any errors.
/eom

Share