Supporting placeholders in the Episerver Forms form submission message

I was recently looking into how to customize the Episerver Forms form submission message (by adding a reference number) to help someone out on the forum. Whilst I was checking it out, just for the sake of it, I wondered how easy it'd be to enable placeholders in the submission message.

As you are probably aware, these are already available in the email templates, so it was just a case of enabling the same placeholders for the submission message. I was envisaging something like this:

How placeholders would look in the Episerver Forms submission message

Well, it turns out it's really easy. All you need to do is replace the DataSubmissionService (EPiServer.Forms.Core.Internal) BuildReturnResultForSubmitAction method and utilize the PlaceHolderService (some details are available in the documentation). Slightly annoyingly, this was the only easy method I could find to override, and the class is internal.

Your custom DataSubmissionService will probably end up looking something like this:

public class CustomDataSubmissionService : DataSubmissionService
{
    private readonly PlaceHolderService _placeHolderService;

    public CustomDataSubmissionService(PlaceHolderService placeHolderService)
    {
        _placeHolderService = placeHolderService;
    }

    protected override SubmitActionResult BuildReturnResultForSubmitAction(bool isJavaScriptSupport, bool isSuccess,
        string message, HttpContextBase httpContext, FormContainerBlock formContainer = null,
        Dictionary<string, object> additionalParams = null, SubmissionInfo submissionInfo = null,
        Submission submission = null, bool isProgressiveSubmit = false, string redirectUrl = "")
    {
        if (!string.IsNullOrEmpty(message))
        {
            var placeHolders = _placeHolderService.GetAllAvailablePlaceHolders(formContainer, httpContext.Request, submission,
                true, false);

            message = _placeHolderService.Replace(message, placeHolders, true);
        }

        return base.BuildReturnResultForSubmitAction(isJavaScriptSupport, isSuccess, message, httpContext,
            formContainer, additionalParams, submissionInfo, submission, isProgressiveSubmit, redirectUrl);
    }
}

Next all you need to do is replace out the default DataSubmissionService:

[InitializableModule]
public class FormsInitialization : IConfigurableModule
{
    public void ConfigureContainer(ServiceConfigurationContext context)
    {
        context.ConfigurationComplete += (o, e) =>
        {
            context.Services.AddSingleton<DataSubmissionService, CustomDataSubmissionService>();
        };
    }

    public void Initialize(InitializationEngine context)
    {
    }

    public void Uninitialize(InitializationEngine context)
    {
    }
}

Now, let's see if this thing actually works:

Example of using placeholders in a form's submission message

Nice! If you just want to customize the submission message the process is basically the same, you can just append whatever you need in the overridden BuildReturnResultForSubmitAction method.

Comments

Very nice. Thanks for sharing

joshua Folkerts