Skip to content

Instantly share code, notes, and snippets.

@LucGosso
Last active May 15, 2017 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucGosso/08ebb442e42cbb7fe239afe63ea95df5 to your computer and use it in GitHub Desktop.
Save LucGosso/08ebb442e42cbb7fe239afe63ea95df5 to your computer and use it in GitHub Desktop.
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