List Manager – Export Contacts with Custom Facets

If you have created custom facets for Sitecore Contacts and want to export the contacts with those custom facets using List Manager, then you are at the right place. In this blog, I am going to share two scenarios to export contacts, wherein the first scenario we are going to include OOTB facet field which is not included in the default export functionality of List Manager and in the second scenario we are going to add the custom facet field to the export functionality of List Manager.

This tutorial is functional for Sitecore 9 or above because it uses xConnect service. To add the OOTB or custom facet fields, create a class that implements IContactDataReader interface. Let’s begin:

// Example for OOTB facet fields
// Adding PhoneNumberList from Sitecore.XConnect.Collection.Model to the CSV file

using Sitecore.ListManagement.XConnect.Web.Export;
using Sitecore.XConnect;
using Sitecore.XConnect.Collection.Model;

namespace Sitecore93.XConnect.Repositories.ListManager.ContactDataReader
{
    public class PhoneNumber : IContactDataReader
    {
        public string FacetName => PhoneNumberList.DefaultFacetKey;

        public string Map(Contact contact)
        {
            var phoneNumberList = contact.GetFacet<PhoneNumberList>(PhoneNumberList.DefaultFacetKey);
            string phoneNumber = $"{phoneNumberList.PreferredPhoneNumber.CountryCode}{phoneNumberList.PreferredPhoneNumber.Number}";
            return phoneNumber;
        }
    }
}
// Custom Facet Definition

using Sitecore.XConnect;
using System;

namespace Sitecore93.XConnect.Models.Facets
{
    [Serializable]
    [FacetKey(DefaultFacetKey)]
    public class ManagerInformation : Facet
    {
        public ManagerInformation() { }
        public string ManagerAccess { get; set; }
        public const string DefaultFacetKey = "ManagerInformation";
    }
}
// Example for Custom Facet fields
// Don't forget to add the reference for Custom Facet definition 

using Sitecore.ListManagement.XConnect.Web.Export;
using Sitecore.XConnect;
using Sitecore93.XConnect.Models.Facets;

namespace Sitecore93.XConnect.Repositories.ListManager.ContactDataReader
{
    public class ManagerAccess : IContactDataReader
    {
        public string FacetName => ManagerInformation.DefaultFacetKey;

        public string Map(Contact contact)
        {
            var managerInformation = contact.GetFacet<ManagerInformation>(ManagerInformation.DefaultFacetKey);
            string managerAccess = managerInformation.ManagerAccess;
            return managerAccess;
        }
    }
}

Now to add the fields to the CSV file, patch the App_Config/Sitecore/ListManagement/Sitecore.ListManagement.config with your own config file.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
    <listManager>
      <export>
        <field name="PhoneNumber" type="Sitecore93.XConnect.Repositories.ListManager.ContactDataReader.PhoneNumber, Sitecore93.XConnect" />
        <field name="ManagerAccess" type="Sitecore93.XConnect.Repositories.ListManager.ContactDataReader.ManagerAccess, Sitecore93.XConnect" />
      </export>
    </listManager>
  </sitecore>
</configuration>

where type is defining the ContactDataReader class for the field which implements IContactDataReader interface and name is the column name.

If you have any thoughts on this, please drop a comment.

4 Comments

    • Hi,

      Please provide more information on what exactly you want to achieve?

      Thanks

      Reply
    • Hi,

      You do not have to deploy any OOTB Sitecore dlls. If you have extended any OOTB functionality, in that scenario reference(s) should be deployed.

      Thanks

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *