0

I'm trying to enable tokens from custom facets inside the RTE for the body in preview, they are working fine in the subject but have not been able to make them work inside the message body.

Here is what I tried, patching this file

<processor type="Sitecore.EmailCampaign.Cm.Pipelines.DispatchNewsletter.SendTestMessage, Sitecore.EmailCampaign.Cm" resolve="true" />

which then calls the TestMessageTask. Inside this class we can see this:

Contact contact = this._contactService.GetContact(recipientInfo.ContactIdentifier, "Personal", "Emails");

Notice the 2 facets used for the contact, while trying to create a custom SendTestMessage I got stuck with some internal and private fields which takes me to this question, is there any way we can preview tokens from custom facets?

1

You have to override the code behind \sitecore modules\Shell\EmailCampaign\UI\Dialogs\MessageBody.aspx (Sitecore.EmailCampaign.Cm.sitecore_modules.Shell.EmailCampaign.UI.Dialogs.MessageBody)

Version I'm looking at: 9.1.1

    using Microsoft.Extensions.DependencyInjection;
    using Sitecore.Data.Items;
    using Sitecore.DependencyInjection;
    using Sitecore.EmailCampaign.Cm.Factories;
    using Sitecore.EmailCampaign.Model.Web.Settings;
    using Sitecore.Framework.Conditions;
    using Sitecore.Globalization;
    using Sitecore.Modules.EmailCampaign.Core;
    using Sitecore.Modules.EmailCampaign.Core.Contacts;
    using Sitecore.Modules.EmailCampaign.Messages;
    using Sitecore.Modules.EmailCampaign.Services;
    using Sitecore.Web;
    using Sitecore.XConnect;
    using System;
    using System.Web.UI;

    namespace Sitecore.EmailCampaign.Cm.sitecore_modules.Shell.EmailCampaign.UI.Dialogs
    {
      public class MessageBody : Page
      {
        private readonly IContactService _contactService;
        private readonly IMessageInfoFactory _messageInfoFactory;
        private readonly IExmCampaignService _exmCampaignService;

        public MessageBody()
          : this(ServiceProviderServiceExtensions.GetService<IContactService>(ServiceLocator.ServiceProvider), ServiceProviderServiceExtensions.GetService<IMessageInfoFactory>(ServiceLocator.ServiceProvider), ServiceProviderServiceExtensions.GetService<IExmCampaignService>(ServiceLocator.ServiceProvider))
        {
        }

        internal MessageBody(
          IContactService contactService,
          IMessageInfoFactory messageInfoFactory,
          IExmCampaignService exmCampaignService)
        {
          Condition.Requires<IContactService>(contactService, nameof (contactService)).IsNotNull<IContactService>();
          Condition.Requires<IMessageInfoFactory>(messageInfoFactory, nameof (messageInfoFactory)).IsNotNull<IMessageInfoFactory>();
          Condition.Requires<IExmCampaignService>(exmCampaignService, nameof (exmCampaignService)).IsNotNull<IExmCampaignService>();
          this._contactService = contactService;
          this._messageInfoFactory = messageInfoFactory;
          this._exmCampaignService = exmCampaignService;
        }

        protected override void OnLoadComplete(EventArgs e)
        {
          if (!Context.User.IsAuthenticated)
            return;
          base.OnLoadComplete(e);
          if (this.IsPostBack)
            return;
          string queryString1 = WebUtil.GetQueryString("message");
          Sitecore.Modules.EmailCampaign.Util.AssertNotNullOrEmpty(queryString1);
          string queryString2 = WebUtil.GetQueryString("lang");
          MessageItem messageItem;
          if (string.IsNullOrEmpty(queryString2))
          {
            messageItem = this._exmCampaignService.GetMessageItem(Guid.Parse(queryString1));
          }
          else
          {
            Language result;
            Language.TryParse(queryString2, out result);
            if (result != (Language) null)
            {
              messageItem = this._exmCampaignService.GetMessageItem(Guid.Parse(queryString1), result.Name);
              messageItem.TargetLanguage = result;
            }
            else
              messageItem = this._exmCampaignService.GetMessageItem(Guid.Parse(queryString1));
          }
          Sitecore.Modules.EmailCampaign.Util.AssertNotNull((object) messageItem);
          ContactIdentifier contactIdentifier = (ContactIdentifier) null;
          string queryString3 = WebUtil.GetQueryString(GlobalSettings.ContactIdentifierSourceQueryStringKey);
          string queryString4 = WebUtil.GetQueryString(GlobalSettings.ContactIdentifierIdentifierQueryStringKey);
          if (!string.IsNullOrWhiteSpace(queryString3) && !string.IsNullOrWhiteSpace(queryString4))
            contactIdentifier = new ContactIdentifier(queryString3, queryString4, ContactIdentifierType.Known);
          if (contactIdentifier != null)
          {







    Contact contact = this._contactService.GetContact(contactIdentifier, "Personal", "Emails", "PhoneNumbers", "<YourFacetKeyNameHere>");







messageItem.PersonalizationRecipient = contact;
          }
          string queryString5 = WebUtil.GetQueryString("targetItem");
          if (!string.IsNullOrEmpty(queryString5))
          {
            Item obj = new ItemUtilExt().GetItem(queryString5);
            if (obj != null)
            {
              WebPageMail webPageMail = messageItem as WebPageMail;
              if (webPageMail != null)
              {
                webPageMail.TargetItem = obj;
                MessageBody.SetDevice(webPageMail);
              }
            }
          }
          MessageInfo messageInfo = this._messageInfoFactory.GetMessageInfo(messageItem);
          messageInfo.FillContentEditorInfo();
          this.Response.Write(messageInfo.Body);
        }

        private static void SetDevice(WebPageMail webPageMail)
        {
          string queryString = WebUtil.GetQueryString("deviceId");
          if (string.IsNullOrEmpty(queryString))
            return;
          Item innerItem = new ItemUtilExt().GetItem(queryString);
          if (innerItem == null)
            return;
          webPageMail.TargetDevice = new DeviceItem(innerItem);
        }
      }
    }
0

For custom tokens to be replaced in the online Preview mode, you must extend the getContact pipeline. To extend the getContact pipeline

  • Create a new pipeline processor extending the GetContact processor:
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[]
            {
                CustomerID.DefaultFacetKey  //all of the custom facets name
            }).ToArray();
 
            args.Contact =
                args.ContactIdentifier != null ?
                    _contactService.GetContact(args.ContactIdentifier, facetKeys)
                    :
                    _contactService.GetContact(args.ContactId, facetKeys);
        }
    }
  • Deploy the DLL to CM and other DDS roles.
  • Replace the Sitecore.Modules.EmailCampaign.Core.Pipelines.GetContact.GetContact pipeline processor in Sitecore.EmailExperience.Core.config file.
<?xml version="1.0"?>
<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">
    <pipelines>
      <group groupName="exm">
        <pipelines>
          <getContact>
            <processor>
              <patch:attribute name="type">DemoSitecore.CustomRulesAndTokens.Tokens.Email.GetContact, DemoSitecore.CustomRulesAndTokens</patch:attribute>
            </processor>
          </getContact>
        </pipelines>
      </group>
    </pipelines>
  </sitecore>
</configuration>

I've blogged down all the steps required for creating a custom token and step 4 of this blog describes how to replace those tokens in online preview mode. Have a look here: https://sitecorewithraman.wordpress.com/2021/01/10/sitecore-exm-custom-tokens/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.