Blog_Sitecore_Segment.jpg

Sitecore Marketing Category Subscription: Part 4 – Custom Contact Identification Field

06.11.2019
Reading time: 2 min

I can now identify a contact and store their preferences as described in Part 3 – Submitting Custom Marketing Preferences.

This theme is something like the cherry on top of the concept. Now that I’m able to identify the contact and play around with its settings, why not do something similar to the settings center?

The links in the newsletter are personalized for the contact, and when a contact clicks the link to update their preferences, their current preferences are selected in the Marketing Preference Center.

I WOULD LIKE TO HAVE THAT TOO!

The selection of the contact’s settings is already done in the view model for fields:

var knownContact = _exmContactService.GetKnownXConnectContactByEmailAddress();
var marketingPreferences = new List<MarketingPreference>();
if (knownContact != null)
{
    marketingPreferences = _marketingPreferencesService.GetPreferences(knownContact, managerRoot.Id);
}

var marketingCategoryGroups = managerRoot.Settings.MarketingCategoryGroups.Select(database.GetItem).ToList();
if (!marketingCategoryGroups.Any())
{
    _logger.LogWarn("no marketing groups are associated to the manager root!");
    return;
}

foreach (var marketingCategoryGroup in marketingCategoryGroups)
{
    var marketingCategories = marketingCategoryGroup.Children;
    foreach (Item marketingCategory in marketingCategories)
    {
        var categoryListItem = new ListFieldItem();
        categoryListItem.ItemId = categoryListItem.Value = marketingCategory.ID.ToString();
        categoryListItem.Text = marketingCategory.DisplayName;
        categoryListItem.Selected = IsSelected(marketingPreferences, marketingCategory);
        Items.Add(categoryListItem);
    }
}

using (new SecurityDisabler())
{
    base.UpdateDataSourceSettings(item);
}

But I only have one form and I don’t want to be able to manipulate the contact’s settings by simply entering an email address. That would be a major security breach!

Hey! Sitecore Forms has the ability to use conditions! Yay!

I created another custom field that returns a value if the contact is a known contact:

public class IsKnownExmContactViewModel : InputViewModel<string>
{
    private readonly IExmContactService _exmContactService;

    public IsKnownExmContactViewModel() : this(ServiceLocator.ServiceProvider.GetService<IExmContactService>())
    {
    }

    public IsKnownExmContactViewModel(IExmContactService exmContactService)
    {
        Condition.Requires(exmContactService, nameof(exmContactService)).IsNotNull();
        _exmContactService = exmContactService;
    }

    protected override void InitializeValue(object value)
    {
        var str = value?.ToString();
        Value = str;
    }

    protected override void InitItemProperties(Item item)
    {
        base.InitItemProperties(item);
        Value = IsKnownContact().ToString();
    }

    private bool IsKnownContact()
    {
        return _exmContactService.GetKnownXConnectContactByEmailAddress() != null;
    }

    protected override void UpdateItemFields(Item item)
    {
        base.UpdateItemFields(item);
        var field = item.Fields["Default Value"];
        field?.SetValue(Value, true);
    }
}

To use conditions and actions, you need to add your custom field to the operators and actions you want to use.
These are located under “/sitecore/system/settings/forms/metadata/conditions/operators” and “/sitecore/system/settings/forms/metadata/conditions/action-types”.

In my case I want to hide a section, which is already possible with the “Hide” action. But I need to compare if my custom field has a boolean value, so I need to add my field in the “Editor Settings” of the “is equal to” operator.

Is equal operator

Holy cow! Now it gets epic!

Now we have to put it all together. ContinNow we have to put everything together. Continue our adventure here: Part 5 – The “magic” subscription form.

Happy Identify!
Dirk

Do you need Sitecore support? – No problem for our Sitecore experts!

The author

Contact