Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Adding custom placeholders for email in Episerver Forms
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment