Get page property from submitting page in #Episerver.Forms

How to read a querystring value from the referrer/hosted form page. Also how to read a page property from the submitting page.

icon of user profile

Published 16th May 2017
For Episerver Version 10 and Forms > 4.4

By implementing IPlaceHolderProvider in the Episerver.Form.UI  Namespace > 4.4 you can use it to add your own implementation of the Placeholder tags in email, eg #yourelement#

You’ll need to add an ExtraPlaceHolder called eg #SomePageProperty# and then implement it under ProcessPlaceHolders.

Important to remember! All placeholders in ProcessPlaceHolders will be triggered on every submit, even if it is not used in that form.

In the submissionData object, you have a SYSTEMCOLUMN_HostedPage item, where the id of the page submitting is saved, this is what we’ll use.

In some scenarios you may want to read the referrer page querystring, this may be used with HttpUtility.ParseQueryString(requestBase.UrlReferrer.Query)[“key”]

!Security warning! Be aware of how you use url parameters. This may be a security risk. Use wisely or not at all.

In my next upcoming blog post I will show how you can save http posted values into hidden formelements, but also querystring values.

using EPiServer;
using EPiServer.Core;
using EPiServer.Forms.Core.Internal;
using EPiServer.Forms.Core.Models;
using EPiServer.ServiceLocation;
using Gosso.EpiserverSite.Mvc.Models.Pages;
using System.Collections.Generic;
using System.Web;

namespace Gosso.EpiserverSite.Mvc.Business.Forms
{
    public class CustomDefaultPlaceHolderProvider : IPlaceHolderProvider
    {
        private readonly IContentRepository contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();

        public new IEnumerable<PlaceHolder> ExtraPlaceHolders
        {
            get
            {
                        public IEnumerable<PlaceHolder> ExtraPlaceHolders => new PlaceHolder[] {
                        new PlaceHolder("PageEmail", string.Empty),
                        new PlaceHolder("OrderInformation", string.Empty),
                        new PlaceHolder("PageUrl", string.Empty) };
            }
        }

        public new IEnumerable<PlaceHolder> ProcessPlaceHolders(IEnumerable<PlaceHolder> availablePlaceHolders, FormIdentity formIden, HttpRequestBase requestBase, Submission submissionData, bool performHtmlEncode)
        {
            // run default
            IEnumerable<PlaceHolder> plh = base.ProcessPlaceHolders(availablePlaceHolders, formIden, requestBase, submissionData, performHtmlEncode);

            var processPlaceHolders = availablePlaceHolders as PlaceHolder[] ?? availablePlaceHolders.ToArray();
            foreach (PlaceHolder holder in processPlaceHolders)
            {
                if (holder.Key == "PageEmail")
                {
                    var email = "web@gosso.se";
                    int id = int.Parse(submissionData.Data["SYSTEMCOLUMN_HostedPage"] + "");

                    PageData containingPage = _contentRepository.Get<PageData>(new ContentReference(id));
                    if (containingPage is TypeWithEmail)
                        email = containingPage.GetPropertyValue<String>("Email","web@gosso.se");

                    holder.Value = email;
                }

                if (holder.Key == "PageUrl")
                {
                    if (requestBase.UrlReferrer != null) holder.Value = requestBase.UrlReferrer.ToString();
                }
                
                if (holder.Key == "OrderInformation")
                {
                    if (requestBase.UrlReferrer != null)
                    {
                        var orderpage = GetOrderPageData(requestBase);
                        if (orderpage!=null)
                            holder.Value = $"Name: {orderpage.PageName} <br/>OrderNo:{((orderpage as TypeWithOrderno) == null ? string.Empty : (orderpage as TypeWithOrderno).OrderNumber)} <br/>Page ID: {orderpage.PageLink.ID.ToString(CultureInfo.InvariantCulture)}";
                    }
                }
            }

            return plh;
        }
        
        private PageData GetOrderPageData(HttpRequestBase requestBase)
        {
            const string orderidname = "orderid";
            string orderref = null;
            if (requestBase.UrlReferrer != null)
            {
                orderref = HttpUtility.ParseQueryString(requestBase.UrlReferrer.Query)[orderidname];//UrlReferrer = {http://localhost:17000/ordersida/?orderid=3296}
            }
            if (orderref == null && requestBase.QueryString[orderidname] != null)
            {
                orderref = requestBase.QueryString[orderidname];
            }
            if (orderref == null)
                return null;
            int orderid = 0;
            int.TryParse(orderref, out orderid);

            return orderid > 0 ? _contentRepository.Get<PageData>(new ContentReference(orderid)) : null;
        }


    }
}

Gist: https://gist.github.com/LucGosso/08ebb442e42cbb7fe239afe63ea95df5

More reading:

SEO terms

  • Extending Episerver forms
  • Customize email