0

I am using Sitecore forms and EXM. I've saved the contact in xdb then I'm using standard Sitecore email campaign message to send an automated email. In this email, I have a put a controller rendering in it and wanted to display all of the inputted fields through this controller.

I am aware that I can use tokens for displaying the input fields, but I would use a controller for future changes. Is there a way to do this?

I think I can get the contact through the Tracking.Current but it's null for some reason when it's on the controller.

Any help would be appreciated.

1 Answer 1

3

There are two parts to this question: Sitecore Forms and EXM.

In order to pass form fields from Sitecore Forms to EXM, you need to create a custom Submit Action that processes the fields on the page and then sends an email using the EXM Client API.

Sitecore Forms

When creating a custom Submit Action, you override the Execute method. Here's an example of that override.

    protected override bool Execute(T data, FormSubmitContext formSubmitContext)
    {
        if (data.MessageId == Guid.Empty)
        {
            logger.LogWarn("Empty message id");
            return false;
        }
        var toContacts = GetToContacts(data, formSubmitContext);
        if (toContacts == null || toContacts.Count == 0)
        {
            return false;
        }
        try
        {
            var customTokens = BuildCustomTokens(data, formSubmitContext);
            foreach (var to in toContacts)
            {
                SendMail(to, customTokens, data.MessageId);
            }
        }
        catch (Exception ex)
        {
            logger.LogError(ex.Message, ex);
            return false;
        }
        return true;
    }

In this example, the developer is creating a Dictionary object called customTokens through the BuildCustomTokens() method that will be passed into the Sitecore EXM Client API, which is called in the SendEmail() method as shown below.

    protected virtual Dictionary<string, object> BuildCustomTokens(T data, FormSubmitContext formSubmitContext)
    {
        var formFields = formFieldConverter.Convert(formSubmitContext.Fields);
        var customTokens = new Dictionary<string, object>();
        customTokens.Add(Constants.CustomTokensFormKey, formFields);
        foreach (var formField in formFields)
        {
            customTokens.Add($"form_{formField.Name}", GetSingleStringValue(formField));
        }
        return customTokens;
    }

    protected virtual void SendMail(ContactIdentifier toContact, Dictionary<string, object> customTokens, Guid messageId)
    {
        var automatedMessage = new AutomatedMessage();
        automatedMessage.ContactIdentifier = toContact;
        automatedMessage.MessageId = messageId;
        automatedMessage.CustomTokens = customTokens;
        automatedMessage.TargetLanguage = Sitecore.Context.Language.Name;
        clientApiService.SendAutomatedMessage(automatedMessage);
    }

Email Experience Manager

Now that the submit action is created, you need to create an EXM Message that has a Rich Text area that can take in the $field_name$ token, which will be replaced, because of the custom token dictionary passed in through the Client API statement.

Credit to Bart Verdonck and this Sitecore Forms Extension module for the code snippets. This module provides the SendEmail submit action as an extension to Sitecore Forms.

Bart Verdonck

5
  • 1
    Looks like you found the code :-) Commented Jul 2, 2019 at 1:55
  • Sssshhh.. =) You see nothing! I did, thanks Dylan. Commented Jul 2, 2019 at 3:31
  • Thanks, this is helpful. However I am using the standard campaign email message using an automated message. In this automated message, I added in a rendering which has some logic in it. In this rendering I wanted to get the contact info using Tracker.Current but the tracker returns NULL. Any ideas to why this is?
    – qwert
    Commented Jul 2, 2019 at 11:47
  • Tracker doesn't get instsntiated in EXM. If you are trying to derive a contact an EXM controller, you have to utilize xConnect and get the contact that way. Your question is talking about getting form data into an email though, so that is where this answer originates from. Commented Jul 2, 2019 at 12:20
  • Oh so the Tracker is expected to be null in EXM. Yeah, I'm trying to get a contact and I used the Tracking.Current.ContactId.ToString("N") as the identifier. So to get the Xconnect contact, I would first need to get the Tracking.Current.ContactId
    – qwert
    Commented Jul 2, 2019 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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