Sitecore 9 Forms: Access landing page fields in FormSubmitContext?

I came across a scenario where I needed to access landing page fields(the page where we add Forms) in Custom Submit Action’s FormSubmitContext. 

By default, the page item isn’t known at the Submit Action. So started thinking should i create a custom hidden field or use the Forms Extensions module? At the end of the day, i didn’t need either. 

Here is the quick way to access the current page using HttpContext’s AbsolutePath.

protected override bool Execute(string data, FormSubmitContext formSubmitContext)
{
 
var contextItem = GetContextItem.GetItem(HttpContext.Current.Request.UrlReferrer?.AbsolutePath) as ILandingPageContentItem;

// Access fields from ContextItem
}

public class GetContextItem
{
	public static IStandardTemplateItem GetItem(string path)
	{
		Item item = Sitecore.Context.Database.GetItem($"/sitecore/content/<tenant>/Home{path}");
		
		return item?.AsStronglyTyped();
	}
}

Hope this helps someone. Any questions, please leave a comment.

Happy Sitecoring!

Leave a Reply

Your email address will not be published. Required fields are marked *