Extending the getContact pipeline

Abstract

Code sample to create a custom personalization token that can be used in Preview mode.

For  custom personalization tokens to be replaced in the online Preview mode, extend the getContactpipeline.

Note

This topic applies to Sitecore versions 9.0.1 and later.

To extend the getContact pipeline:

  1. Create a new pipeline processor.  See example below.

  2. Deploy the assembly with your pipeline processor to all CM and DDS instances.

  3. Navigate to the \App_Config\Sitecore\EmailExperience directory.

  4. Create a configuration patch file, in which you replace the Sitecore.Modules.EmailCampaign.Core.Pipelines.GetContact.GetContact pipeline processor in the getContact pipeline in Sitecore.EmailExperience.Core.config file.

The following example illustrates how to extend the getContact pipeline:

using System;
using System.Linq;
using Sitecore;
using Sitecore.Data;
using Sitecore.Framework.Conditions;
using Sitecore.Modules.EmailCampaign.Core.Contacts;
using Sitecore.Modules.EmailCampaign.Core.Pipelines.GetContact;

namespace MyNamespace
{
    public class GetContact
    {
        private readonly IContactService _contactService;

        public GetContact([NotNull] IContactService contactService)
        {
            Condition.Requires(contactService, nameof(contactService)).IsNotNull();

            _contactService = contactService;
        }

        public void Process([NotNull] GetContactPipelineArgs args)
        {
            Condition.Requires(args, nameof(args)).IsNotNull();

            if (args.ContactIdentifier == null && ID.IsNullOrEmpty(args.ContactId))
            {
                throw new ArgumentException("Either the contact identifier or the contact id must be set");
            }

            string[] facetKeys = args.FacetKeys.Concat(new[]
            {
                "CustomFacetName" // the custom facet name
            }).ToArray();

            args.Contact =
                args.ContactIdentifier != null ?
                    _contactService.GetContact(args.ContactIdentifier, facetKeys)
                    :
                    _contactService.GetContact(args.ContactId, facetKeys);
        }
    }
}