Using dynamic success URLs in Sitecore Form submit

Consider the scenario: The website has a knowledge base, where there are several Knowledge Articles available for the user to refer. Each article is also associated with additional downloadable content in Sitecore. The downloadable content is to be gated. Each Knowledge Article has a common Sitecore form – which collects user information and on form submission, redirects the user to a common downloads page, where the assets of the current article are available for download. 

  • In this case, we went with a single Sitecore form, since this was on a multi-instance template of an article, and we needed to ensure that the presentation on standard values of the template holds good and did not need updates on individual items. 
  • Additionally, again, since Article is a multi-instance template, with unlimited items, we did not want to have to create a separate form for each article 

Due to this, we landed on the use of a single Downloads page – as the redirect on success page. 

Having this decision in place, we needed a way to show the link to the downloads of the article from which the form was submitted on this common download page.  

There were 4 parts to this implementation 

  • Embed the page context item on the form 
  • Create a custom submit action, inheriting RedirectToPage action, to append this context item id to the redirect link – thus making the success URL dynamic 
  • Use this item id on the component placed on this fixed success page component, and show the link to the downloads from the corresponding item 
  • As an additional step, we ensured that if a user landed on this page, from any page other than a knowledge article page, simply redirect them to the home / landing page – thus blocking access to the downloadable content without having filled the form. 

You can find information regarding embedding context item id in your Sitecore form in this post: Access Context Item on Sitecore Forms using a Value Provider

Following is the code for the custom action inheriting from the built-in RedirectToPage action, to append the context item id to the redirect url, if available in form fields. This could be used in tandem with the content hidden field on any form! 

using Sitecore; 
using Sitecore.Configuration; 
using Sitecore.Data; 
using Sitecore.Data.Items; 
using Sitecore.Diagnostics; 
using Sitecore.ExperienceForms.Models; 
using Sitecore.ExperienceForms.Processing; 
using Sitecore.ExperienceForms.Processing.Actions; 
using Sitecore.ExperienceForms.Processing.Actions.Models; 
using Sitecore.Links; 
using Sitecore.Text; 
using System; 
using System.Linq; 
  
namespace sc101.Foundation.Cms.Forms 
{ 
    public class CustomRedirectToPage : RedirectToPage 
    { 
        public CustomRedirectToPage(ISubmitActionData submitActionData) : base(submitActionData) 
        { 
        } 

        protected override bool Execute(RedirectActionData data, FormSubmitContext formSubmitContext) 
        { 
            Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext)); 
            if (data == null || data.ReferenceId == Guid.Empty) 
                return false; 
            Item obj = Context.Database.GetItem(new ID(data.ReferenceId)); 
            if (obj == null) 
                return false; 
            UrlOptions defaultUrlOptions = LinkManager.GetDefaultUrlOptions(); 
  
            Item contextItem = null; 
            if (formSubmitContext.Fields.Any(f => f.Name.ToLower() == "context") 
            && ID.TryParse((formSubmitContext.Fields.First(f => f.Name.ToLower() == 
 context")).Value, out ID contextId)) 
            { 
                contextItem = Context.Database.GetItem(contextId); 
            } 
            defaultUrlOptions.SiteResolving = Settings.Rendering.SiteResolving; 
            formSubmitContext.RedirectUrl = new UrlString(LinkManager.GetItemUrl(obj, defaultUrlOptions) + "?item=" + contextItem?.ID.Guid.ToString("D")).ToString(); 
            formSubmitContext.RedirectOnSuccess = true; 
            formSubmitContext.Abort(); 
            return true; 
        } 
    } 
} 

Create the custom action in Sitecore, at /sitecore/system/Settings/Forms/Submit Actions, using template /sitecore/templates/System/Forms/Submit Action 

On the form itself, we simply select the custom action, and configure it with the common Download page – as we would if we used the OOTB RedirectToPage action. 

The last step here was to update the code of the download component to read this querystring parameter and output download links accordingly using simple code like this. 

IKnowledgePage currentKnowledgePage = _mvcContext.SitecoreService.GetItem<IKnowledgePage>(Request.QueryString["item"]); 

That’s it, the implementation is now complete! 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s