SITECORE LIST MANAGER ( Part 4 ) : Extend the view of the Contact List to see Custom Facets.

Sitecore Version: 9.3

  • Once we have extended the List manager to import and export custom facets using CSV, we have an option to extend the view of the contact list to see custom facets instead of the default ones. 

  • By default, we can only see, First Name, Last Name, and email facets values. In our case, we have a custom facet “Customer Status” associated with all contacts.

  • To achieve that, we’ll need to extend
    Sitecore.ListManagement.Services.Repositories.ListSubscriptionsStor
    e
    class and patch the custom routes created for this in
    Sitecore.Mvc.Pipelines.Loader.InitializeRoutes.

    Following are the steps in detail:

  1. Create and deploy a custom model that wraps a custom facet, as described in this article.

  2. Add a column for a custom facet to the Contacts table.

    • Switch to the Core database.

    • Add a new ColumnField item under the /sitecore/client/Applications/List Manager/Lists/Contact list item.

    • In the newly created item, fill out the following fields:

      • EmptyText (sets a default text if a cell does not contain any data.
      • HeaderText (a name of the column)
      • DataField (a name of the custom facet, used as a data source).

    • Save the changes. The new column then appears in the List Manager app.

3. Configure the Import and Export contacts wizard dialog to include custom contact facets as described here.

4. Create a custom contacts controller.

  • Create your custom ContactDataModel to contain all the required facets.


  • Data is mapped by the Sitecore.ListManagement.Services.Repositories.ListSubscriptionsStoreclass.

  • Create your own class that has a GetSubscribers method returning IEnumerable of your custom model as follows:



  • The data is retrieved from _contactProvider and converted to the specified model using the MapEntity method.

    Next, inject the custom mapping logic in the MapEntity method:


  • The _contactProvider.GetFilteredContacts method returns only three facets (ListSubscriptions, Personal, Emails).

    So, add the custom ones to the list:


  • The default ListSubscriptionsStore is used by the Sitecore.ListManagement.Services.Controllers.ContactsController  controller. 

  • Create your custom controller, which performs the same actions and changes the GetEntries method to use the custom ListSubscriptionsStore and Change the RoutePrefix attribute value of the controller to sitecore/api/customlists/{listId}/contacts.


using MA.CustomMarketingApps.Model;
using Microsoft.Extensions.DependencyInjection;
using Sitecore.Abstractions;
using Sitecore.ListManagement.Operations;
using Sitecore.ListManagement.Providers;
using Sitecore.ListManagement.XConnect;
using Sitecore.ListManagement.XConnect.Segmentation;
using Sitecore.ListManagement.XConnect.Web;
using Sitecore.Services.Infrastructure.Web.Http;
using System;
using System.Collections.Generic;
using Sitecore.ListManagement.DependencyInjection;
using Sitecore.Marketing.Definitions;
using Sitecore.Marketing.Definitions.Segments;
using Sitecore.Marketing.Definitions.ContactLists ;
using Sitecore.Data.Managers;
 
namespace MA.CustomMarketingApps.Controllers
{
    [System.Web.Http.RoutePrefix("sitecore/api/customlists/{listId}/contacts")]
    public class CustomContactsController: ServicesApiController
    {
        private readonly Model.IListSubscriptionsStore<Model.ContactDataModel> _listSubscriptionsStore;
        private readonly BaseLog _log;
         
        public CustomContactsController()
        {
              
            IContactListProvider clp = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<IContactListProvider>();
            var sp = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<ISubscriptionService>();
            var csf = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<IContactSourceFactory>();
            var sss = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<ISegmentationService>();
            var cp = new MA.CustomMarketingApps.ContactProvider(sss, csf); // create an object of the custom ContactProvider class
            var or = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<IListOperationRepository>();
            var lss = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<Model.IListSubscriptionsStore<Model.ContactDataModel>>();
            int batchSize = Sitecore.Configuration.Settings.GetIntSetting("ListManagement.BatchSize", 250);
            this._listSubscriptionsStore = new ListSubscriptionsStore(clp, sp, cp, or, batchSize, csf, sss); // create your custom store
        }
 
        [System.Web.Http.Route]
        [System.Web.Http.HttpGet]
        [System.Web.Http.ActionName("customaction")]
        public virtual IEnumerable<Model.ContactDataModel> GetEntities(Guid listId, string filter = " ", int pageIndex = 0, int pageSize = 20)
        {
            return this._listSubscriptionsStore.GetSubscribers(listId, filter, pageIndex, pageSize); // your custom store must be used here
        }
    }
}


}
  • Create a processor that registers your control:

    using Sitecore.Pipelines;
    using System.Web.Http;
    using System.Web.Routing;
     
    namespace MA.CustomMarketingApps.Model
    {
        public class RegisterHttpRoutes
        {
            public void Process(PipelineArgs args)
            {
                GlobalConfiguration.Configure(Configure); 
            }
     
            protected void Configure(HttpConfiguration configuration) 
            {
                var routes = configuration.Routes;
                routes.MapHttpRoute("CustomContacts", "sitecore/api/customlists/{listId}/contacts", new 
                {
                    controller = "CustomContacts",
                    action = "customaction", // Name of the action in ActionName attribute         
                });
            }
        }
    }
  • Build the assembly and put it in the bin folder of your site.

  • Patch the “RegisterHttpRoutes” processor right after “<processor type=”Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc” /> ” (<initialize> pipeline).

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <initialize>
        <processor type="MA.CustomMarketingApps.Model.RegisterHttpRoutes, MA.CustomMarketingApps"/>
      </initialize>
    </pipelines>
  </sitecore>
</configuration>

  • Open your Core database in the Content Editor and navigate to the /sitecore/client/Applications/List Manager/Global Settings/ListTaskPageSettings/ContactsDataSource Parameters item.

    Change its URL field to “/sitecore/api/customlists”.

  • After performing all these steps, you can upload a CSV list containing custom facets.

Happy Sitecoring !!

Comments

  1. https://sitecorewithraman.wordpress.com/2021/07/01/customize-list-manager-part-iii/

    ReplyDelete

Post a Comment