Customize Summary in emails from #Episerver Forms

Episerver Forms is getting stronger. Here is a example of the how to customize the summary text with PlaceHolderProvider that is available in version 4.4.

icon of user profile

Published 10th February 2017
For Episerver Version 10 and Forms 4.4

PlaceholderProvider in the Episerver.Form.UI  Namespace >4.4 can be used to override the processing of the Placeholder tags #yourelement#

Placeholders are handled with PlaceHolderService. You can replace PlaceHolderService with your own implementation with Dependency Injection.

But in this example i will extend the DefaultPlaceHolderProvider
because i want to reuse the code and override some methods.

DefaultPlaceHolderProvider is the default implementation of IPlaceHolderProvider, which replaces FormElementPlaceHolders with an actual value and provides an extra #SUMMARY# placeholder.

I will add an ExtraPlaceHolder called #FriendlySummary#, that takes away all hidden fields.

Why Changing the summary:

Editors may use summary in every email when confirming the submission to the user. When they used hidden element, they also where showed in summary, and we don’t want that.

Solution:

A form element that is implementing IExcludeInSubmission is automaticly not displayed in Summary, like the captcha element, but IExcludeInSubmission not applicable in this case.

My condition: All form elements with name starting with “Hidden… “ like “Hidden IP” or “Hidden VisitorGroup is returning user” or “Hidden anything” will not be displayed in my #FriendlySummary#

But you can also exclude PredefinedHiddenElementBlock or whatever block…

The form with hidden fields:

 

 

The email message:

The emailed result:

The idea in this example is to replace the value shouldNotShowInSummary with a real code, could be done in this provider code.

The Code example

using EPiServer.Forms.Core;
using EPiServer.Forms.Core.Internal;
using EPiServer.Forms.Core.Models;
using EPiServer.Forms.Core.Models.Internal;
using EPiServer.ServiceLocation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Gosso.EPiServerTestSite.Business
{
    public class CustomDefaultPlaceHolderProvider : DefaultPlaceHolderProvider, IPlaceHolderProvider
    {
        private readonly Injected<IFormRepository> _formRepository;

        public new IEnumerable<PlaceHolder> ExtraPlaceHolders
        {
            get
            {
                return new PlaceHolder[] { new PlaceHolder("friendlysummary", string.Empty) };
            }
        }

        public new IEnumerable<PlaceHolder> ProcessPlaceHolders(IEnumerable<PlaceHolder> availablePlaceHolders, FormIdentity formIden, HttpRequestBase requestBase, Submission submissionData, bool performHtmlEncode)
        {
            IEnumerable<PlaceHolder> plh = base.ProcessPlaceHolders(availablePlaceHolders, formIden, requestBase, submissionData, performHtmlEncode);
            Type[] excludedElementBlockTypes = new Type[] { typeof(IExcludeInSubmission)/*, typeof(EPiServer.Forms.Implementation.Elements.PredefinedHiddenElementBlock)*/ };
            IEnumerable<FriendlyNameInfo> friendlyNameInfos = this._formRepository.Service.GetFriendlyNameInfos(formIden, excludedElementBlockTypes);

            foreach (PlaceHolder holder in availablePlaceHolders)
            {
                if (holder.Key == "friendlysummary")
                {
                    holder.Value = this.GetFriendlySummaryTextWithoutHiddenFields(friendlyNameInfos, performHtmlEncode ? "<br />" : Environment.NewLine, requestBase, submissionData);
                }
            }

            return plh;
        }
        
        //// Overriding default SummaryText
        //protected new string GetFriendlySummaryText(IEnumerable<FriendlyNameInfo> friendlyNames, string lineSeparator, HttpRequestBase requestBase, Submission submissionData)
        //{
        //    //IEnumerable<FriendlyNameInfo> names = friendlyNames.Where(u => !u.FriendlyName.ToLowerInvariant().StartsWith("hidden")).ToList();//remove hidden fields
        //    var str = "Hijacked! <br/>" + base.GetFriendlySummaryText(friendlyNames, lineSeparator, requestBase, submissionData);
        //    return str;
        //}

        protected string GetFriendlySummaryTextWithoutHiddenFields(IEnumerable<FriendlyNameInfo> friendlyNames, string lineSeparator, HttpRequestBase requestBase, Submission submissionData)
        {
            IEnumerable<FriendlyNameInfo> names = friendlyNames.Where(u => !u.FriendlyName.ToLowerInvariant().StartsWith("hidden")).ToList();//remove hidden fields
            var str = "Hidden fields is not shown <br/>" + base.GetFriendlySummaryText(names, lineSeparator, requestBase, submissionData);
            return str;
        }
    }
}
Summary:

Episerver Forms is getting better and better, PlaceholderProvider is great news. Even better would be to add a new property on BaseElement called “HideInSummary” by default.

Gist:

Source:

SEO Terms:

  • SendEmailAfterSubmissionActor
  • Replacing input fields with custom data after submit
  • Changing field output in email
  • Customize Summary placeholder