Sitecore CMP connector extension to map link fields from Sitecore Content Hub to XP

 

Versions Used: Sitecore Content Hub: v3.4, Sitecore XP: v9.1 Update 1, Sitecore CMP Connector: v2.0

This blog post will explain the CMP connector extension (Source Code here) for mapping and synchronizing link fields defined in Sitecore Content Hub CMP content types to Sitecore XP Generic links field.

This connector extends the OOTB CMP connector v2.0 by adding a new pipeline to process the link fields of a new template type /sitecore/templates/CMP/Link Field Mapping.

 <pipelines>       
       <cmp.importEntity>  
         <processor type="Sitecore.CMP.Connector.Extensions.LinkField.Pipelines.SaveLinkFieldValues, Sitecore.CMP.Connector.Extensions.LinkField" patch:after="processor[@type='Sitecore.CMP.Connector.Extensions.LinkField.Pipelines.SaveImageFieldValues, Sitecore.CMP.Connector.Extensions.LinkField']" resolve="true" />  
       </cmp.importEntity>  
     </pipelines>  

This template has the following fields:





The top 2 field maps to CMP content label and text while the 3rd field maps to the Generic link field in Sitecore XP.

The pipeline processor (SaveLinkFieldValues.cs) fetches all Link Field Mapping items from ImportEntityPipelineArgs object under the Entity Mapping item being processed, reads required field values, and stores the formatted link into the specified Sitecore field.

Please note that the processor works for both external and internal links with Sitecore XP.

 foreach (var item in from i in args.EntityMappingItem.Children  
                        where i.TemplateID == Constants.LinkFieldMappingTypeId  
                        select i)  
             {  
               var cmpLinkLabelFieldName = item[Constants.LinkFieldMappingCmpLinkLabelFieldNameFieldId];  
               var cmpLinkFieldName = item[Constants.LinkFieldMappingCmpLinkFieldNameFieldId];  
               var sitecoreFieldName = item[Constants.LinkFieldMappingSitecoreFieldNameFieldId];  
               try  
               {  
                 var linkLabel = args.Entity.GetPropertyValue<string>(cmpLinkLabelFieldName);  
                 var link = args.Entity.GetPropertyValue<string>(cmpLinkFieldName);  
                 if (!string.IsNullOrEmpty(link))  
                 {  
                   LinkType linkType;  
                   if (link.ToLowerInvariant().StartsWith("http") || link.ToLowerInvariant().StartsWith("www"))  
                     linkType = LinkType.External;  
                   else  
                     linkType = LinkType.Internal;  
                   args.Item[sitecoreFieldName] = GetLinkElement(link, linkLabel, linkType);  
                 }  
               }  
               catch (Exception ex)  
               {  
                 this.Logger.Error(Helper.GetLogMessageText(  
                   $"An error occurred during converting '{(object) cmpLinkLabelFieldName}' field to '{(object) sitecoreFieldName}' field. Image Field mapping ID: '{(object) item.ID}'."), ex, (object)this);  
               }  
             }  
References: https://github.com/vchandrasn/Sitecore.CMP.Connector.Extensions.LinkField

Comments