SITECORE LIST MANAGER ( Part 3 ) : Import custom facet as a Sitecore mapping field

we will see how to extend the Import contacts wizard to display a custom facet as a Sitecore mapping field and how to create a new import model field for the Custom facet in the Content Editor.

To add a new facet as a mapping field:

  • In the Core database, in the Content Editor, navigate to the /sitecore/client/Applications/List Manager/Dialogs/ImportWizardDialog/PageSettings/TabControl Parameters/Map/ImportModel folder.

  • Create a new Import model field item based on the /sitecore/client/Applications/List Manager/Templates/ImportModelField template.

    To do this, in the
    ImportModel folder, duplicate one of the existing items and name this CustomerStatus Status

  • To modify the new item, in the FieldName field, specify the name of the Sitecore mapping field to display in the Import contacts wizard.

  • In the DataField field, specify the key that the Facet Mapper uses to recognize the xConnect facet property of where to store the imported value.
  • For example, name it CustomerStatus_Status and later use this key to fill the Status field of the CustomerStatus facet:

  • Now we will create a custom import mapper class.
  • Create a new CustomerFacetMapper that implements Sitecore.ListManagement.XConnect.Web.Import.IFacetMapperSitecore.ListManagement.Import.IFacetMapper:


using MA.CustomFacets.Model;
using Sitecore.Diagnostics;
using Sitecore.ListManagement.Import;
using Sitecore.ListManagement.XConnect.Web.Import;
using Sitecore.XConnect;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Import Sitecore.ListManagement.XConnect.Web
namespace MA.CustomMarketingApps.Mappers
{
    class CustomerStatusFacetMapper : IFacetMapper
    {
        private const string FacetMapperPrefix = "CustomerStatus_";

        public CustomerStatusFacetMapper() : this("CustomerStatus")
        {
        }
        public CustomerStatusFacetMapper(string facetName)
        {
            Assert.ArgumentNotNull(facetName, nameof(facetName));

            this.FacetName = facetName;
        }
        public string FacetName { get; }

        public MappingResult Map(string facetKey, Facet facet, ContactMappingInfo mappings, string[] data)
        {
            using (EventLog eventLog = new EventLog("Application"))
            {
                eventLog.Source = "Application";
                eventLog.WriteEntry("CustomerFacetMapperCalled facetKey:" + facetKey + " data" + string.Join("; ", data));
            }

            if (facetKey != this.FacetName)
            {
                return new NoMatch(facetKey);
            }

            CustomerStatus customeruser = new CustomerStatus();
            string customerStatus = mappings.GetValue(FacetMapperPrefix + nameof(customeruser.Status), data);
            Log.Info("Facet Mapper: " + FacetMapperPrefix + nameof(customeruser.Status), this);
            if (!string.IsNullOrEmpty(customerStatus)) { customeruser.Status = customerStatus; }
            return (MappingResult)new FacetMapped(facetKey, (Facet)customeruser);

        }
    }
}

  • Build this solution and deploy the DLL to the Sitecore root bin folder.

  • In the Sitecore.ListManagement.config file (/App_Config/Sitecore/ListManagement), under the sitecore/import/facetMapper section, register the mapper.

    To do that, I’ve created a patch:


<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"
               xmlns:role="http://www.sitecore.net/xmlconfig/role/">
  <sitecore role:require="Standalone or ContentManagement">
    <settings>
      <import>
        <facetMapper type="Sitecore.ListManagement.XConnect.Web.Import.CompositeFacetMapperCollection, Sitecore.ListManagement.XConnect.Web">
          <param resolve="true" type="Sitecore.Abstractions.BaseLog, Sitecore.Kernel"/>
          <facetMappers hint="list:Add">
            <facetMapper type="MA.CustomMarketingApps.Mappers.CustomerStatusFacetMapper, MA.CustomMarketingApps" />
          </facetMappers>
        </facetMapper>
      </import>
    </sitecore>
</configuration>


  • In the Sitecore.ListManagement.config file, add the CustomerInterest facet name to extend the list of facets to map to.


<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"
               xmlns:role="http://www.sitecore.net/xmlconfig/role/">
  <sitecore role:require="Standalone or ContentManagement">
    <settings>
      <setting name="ListManagement.Import.FacetsToMap" value="Emails|Personal|Addresses|CustomerStatus" />
    </settings>
  </sitecore>
</configuration>

  • Now in the ListManager dashboard, create a list from the file. And browse for CSV (CSV should have data against headers Email, FirstName, LastName, NickName, CustomerStatus), Map the fields.

  • Now to enable the export of custom facets to a CSV file, we need to create


using MA.CustomFacets.Model;
using Sitecore.ListManagement.XConnect.Web.Export;
using Sitecore.XConnect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace MA.CustomMarketingApps.Mappers
{
    class CustomerStatusContactDataReader : IContactDataReader
    {
        public string FacetName
        {
            get
            {
                return "CustomerStatus";
            }
        }

        public string Map(Contact contact)
        {
            string CustomerFacetKey = CustomerStatus.DefaultFacetKey;
            return contact.GetFacet<CustomerStatus>(CustomerFacetKey)?.Status ?? "";
        }
    }
}

  • Open the App_Config\Include\ListManagement\Sitecore.ListManagement.config file and in the listManager.export pipeline, register the new one:


<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"
               xmlns:role="http://www.sitecore.net/xmlconfig/role/">
  <sitecore role:require="Standalone or ContentManagement">
    <listManager>
      <export>
        <field name="CustomerStatus" type="MA.CustomMarketingApps.Mappers.CustomerStatusContactDataReader, MA.CustomMarketingApps" />
      </export>
    </listManager>
  </sitecore>
</configuration>

  • Now check if data for the newly added field is getting exported correctly. Click on Export contact. CSV will download and you’ll see Customer Status data is there

That is all :)

Comments

  1. https://sitecorewithraman.wordpress.com/2021/03/27/customize-list-manager-part-ii/

    ReplyDelete

Post a Comment