Sitecore EXM

Recently we faced an interesting Sitecore EXM issue. We can create an email with a name of 100 chars long. The default maxLength is 100. Once we create the email, we can find it in the content tree with the same name. During this process, there is no error.

However, when we trigger to send the email, we found our email is moving to in-progress / paused state. And in the EXM log we found the following errors:

ManagedPoolThread #12 10:49:22 INFO Dispatch Message (TEST DEO UGRAD - Officer Selection Board - OSB Sep 27 - 1 Oct 2021 - Test): Started
Message Id: {E14EC76B-8BE4-4156-8316-5333FB7FE1F7}
Message Path: /sitecore/content/Email/Messages/2021/09/16T061053/TEST DEO UGRAD - Officer Selection Board - OSB Sep 27 - 1 Oct 2021 - Test
Included Recipient Lists: Test Liston 02-06-2021
Excluded Recipient Lists:

ManagedPoolThread #12 10:49:22 ERROR Exception: Sitecore.Exceptions.InvalidItemNameException
Message: An item name lenght should be less or equal to 100.
Source: Sitecore.Kernel
at Sitecore.Data.Items.ItemUtil.AssertItemName(Item destinationItem, String name)
at Sitecore.Data.Items.Item.Add(String name, TemplateID templateID)
at Sitecore.Modules.EmailCampaign.Core.ItemUtilExt.AddSubItemFromTemplate(String name, String displayName, TemplateID templateId, Item parent)
at Sitecore.EmailCampaign.Cm.Pipelines.DispatchNewsletter.DeployAnalytics.AddCampaignItem(MessageItem message)
at Sitecore.EmailCampaign.Cm.Pipelines.DispatchNewsletter.DeployAnalytics.ProcessCampaign(MessageItem message)
at Sitecore.EmailCampaign.Cm.Pipelines.DispatchNewsletter.DeployAnalytics.Process(DispatchNewsletterArgs args)

The error message is surprising because it is coming from Sitecore Kernel and complaining about the item name length is more than 100, while our email name length is 74. Another thing that is noticeable is the error initiated from “Sitecore.EmailCampaign.Cm.Pipelines.DispatchNewsletter.DeployAnalytics“.

So, I have opened the source code and found the following:

private Item AddCampaignItem(MessageItem message)
{
	Item item = null;
	if (message.MessageType != MessageType.Undefined)
	{
		item = _itemUtil.GetItem(message.CampaignPosition);
	}
	if (item == null)
	{
		item = _itemUtil.GetItem("{C1B8669B-C41A-4ED3-BF0D-E859CF880D42}");
	}
	Util.AssertNotNull(item);
	Item destinationFolderItem = _itemUtil.GetDestinationFolderItem(item);
	Util.AssertNotNull(destinationFolderItem);
	destinationFolderItem = _itemUtil.GetItem(destinationFolderItem.ID, message.InnerItem.Language, allowOtherLangs: false);
	Util.AssertNotNull(destinationFolderItem);
	Item item2 = _itemUtil.AddSubItemFromTemplate($"{message.InnerItem.Name}{message.MessageId:D}", message.InnerItem.DisplayName, new TemplateID(AnalyticsIds.Campaign), destinationFolderItem);
	Util.AssertNotNull(item2);
	item2.Editing.BeginEdit();
	item2[new ID("{0F47D1B4-BCB4-4F7F-9065-6ECB46AAB9A1}")] = item2.Name;
	item2[new ID("{43AA1C9A-D1BE-498A-9841-1DEF2C2231BA}")] = "{3E11F7A7-0059-45EF-95D9-3B03425B3BAF}";
	if (item2.Fields.Contains(new ID("{47B4D72E-5DFD-44B4-BD9B-8C360363F887}")))
	{
		item2[new ID("{47B4D72E-5DFD-44B4-BD9B-8C360363F887}")] = "{EB042E92-6C2A-4A65-92EE-F74D21B3107D}";
	}
	DateTime dateTime = _currentDateProvider.UtcNow();
	item2[new ID("{BD4720D7-9ED6-4438-959B-7B1C5C32C10B}")] = DateUtil.ToIsoDate(dateTime);
	item2.Editing.EndEdit();
	message.Source.CampaignId = item2.ID;
	message.Source.StartTime = dateTime;
	return item2;
}

Sitecore is trying to create a Campaign for analytics in the background. In line 18, we can see it is actually combining  “Email Item.Name + MessageId“. Now, if our email name length is 74 and the top of that MessageId (GUID) length is 36, it is crossing the MaxItemNameLength.

During the above process, it is executing “Sitecore.Data.Items.ItemUtil.GetItemNameError” from Sitecore Kernel, where the following validation is making our email send fail. (typo in the text “length” is from the source code)

if (name.Length > Settings.MaxItemNameLength)
{
	return Translate.Text($"An item name lenght should be less or equal to {Settings.MaxItemNameLength}.");
}

So, it is clear the issue is a Sitecore bug. We figured out 3 different solutions.
1. Override the “Sitecore.EmailCampaign.Cm.Pipelines.DispatchNewsletter.DeployAnalytics” and fix the campaign name.
2. Put a validation when the user enters an email name larger than 64 chars.
3. Increase the Sitecore “MaxItemNameLength“.

To proceed with the first approach, we were concerned about the suffix of the Campaign name, which is the MessageId. Might have some other use internally. It looks like the following

The second approach can be achieved by setting the maxLength in Sitecore core. Following are the steps:
i. Select the core database and browse this item in the content tree “sitecore/client/Applications/ECM/Pages/Messages/Regular/PageSettings/Tabs/General“.
ii. (Thanks to Maarten for this) Open the presentation details, find the first textbox rendering on it, the ID should be “NameTextBox” and change the MaxLength. It looks like below:

The third approach is very straight -forward, just change the MaxItemNameLength in Sitecore.config or create a patch to override it.

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
    <settings>
      <setting name="MaxItemNameLength" set:value="150" />
    </settings>
  </sitecore>
</configuration>

We raised a Sitecore ticket and we got the following reply:

We have registered this behavior as a bug.
To track the future status of this bug report, please use reference number 365930. More information about public reference numbers can be found here: https://kb.sitecore.net/articles/853187

As a workaround, please increase the value of MaxItemNameLength in Sitecore.config. For example, by changing the value to 150. Or you can shorten the message name as what you have mentioned previously.

Thanks
Munir

Previous post Dialogflow Custom Payload for Facebook Messenger
Next post PowerShell scripts to get Sitecore User information