WFFM WebFroms Customizing the SuccessRedirect Pipeline

Like any other pipelines in Sitecore, WFFM pipelines are also customizable. I am showing you here how to customize the WFFM WebForms SuccessRedirect pipeline.

The SuccessRedirect pipeline is used to process the Success page URL which you provide on the form under the success action. This pipeline is responsible to process the URL provided by the user and can process internal, external and media URLs.

You can find SuccessRedirect class under the Sitecore.Form.Core.Pipelines.FormSubmit namespace

in the below example, I am going to add a QueryString to the success page URL. I’ll read a cookie and use its value for QueryString.

See the example here

namespace SitecoreTricks.Helper.Pipelines
{
   public class SuccessRedirect:ClientPipelineArgs
   {
     public void Process(SubmitSuccessArgs args)
     {
       Assert.IsNotNull((object)args, "args");
       if (args.Form != null)
       {
         if (!args.Form.SuccessRedirect)
              return;
         args.AbortPipeline();
         LinkField successPage = args.Form.SuccessPage;
         UrlString urlString = (UrlString)null;
         if (successPage.LinkType == "external")
             urlString = new UrlString(successPage.Url);
         else if (successPage.TargetItem != null)
         {
            switch (successPage.LinkType)
            {
               case "internal":
                    UrlOptions defaultUrlOptions = LinkManager.GetDefaultUrlOptions();
                    defaultUrlOptions.SiteResolving = Settings.Rendering.SiteResolving;
                    urlString = new UrlString(LinkManager.GetItemUrl(successPage.TargetItem, defaultUrlOptions));
                    break;
              case "media":
                   urlString = new UrlString(MediaManager.GetMediaUrl(new MediaItem(successPage.TargetItem)));
                   break;
            }
        }
        else
           DependenciesManager.Logger.Warn(string.Format("Web Forms for Marketers : Success page for the form does not exist. Form ID: '{0}'.", (object)args.Form.ID), new object());

        if (urlString == null)
            return;
        string attribute = successPage.GetAttribute("querystring");
        if (!string.IsNullOrEmpty(attribute))
             urlString.Parameters.Add(WebUtil.ParseUrlParameters(attribute));
        HttpCookie cookie = HttpContext.Current.Request.Cookies["testCookie"];
        if (cookie != null)
        {
           var finalUrl = urlString + "?" + cookie.Value;
           cookie.Expires = DateTime.Now.AddDays(-1);
           WebUtil.Redirect(finalUrl, false);
        }
        else
        {
           WebUtil.Redirect(urlString.ToString(), false);
        }
        WebUtil.Redirect(urlString.ToString(), false);
     }
     else
     {
       Uri result;
       if (!Uri.TryCreate(args.Result, UriKind.RelativeOrAbsolute, out result))
           return;
       try
       {
          string pathAndQuery = result.PathAndQuery;
       }
       catch (Exception ex)
       {
         return;
       }
       args.AbortPipeline();
       WebUtil.Redirect(args.Result, false);
     }
   }
 }
}

Once you are done with the code customizations then you need to make a config change. You need to find the Sitecore.Forms.Config file and navigate to <successAction> tag under the pipelines. Paste your custom processor there.

See the below example

<processor type=”SitecoreTricks.Helper.Pipelines.SuccessRedirect, SitecoreTricks.Helper”/>

and that’s it. you are done with your WFFM pipeline customization.

Downloading the Media item on WFFM from submission

I have seen that many developers are searching for a solution for downloading the media item on successful submission of a WFFM form, But I couldn’t find an exact solution over the internet and that’s where I thought of putting this to help the community.

So here is the solution.

  1. Add a field to your form and hide it using the tricks I explained in one of my previous posts. You can find that here- Hide a field on WFFM (No CSS & JS)
  2. Give that field a default value and that value should be the Media Item Id.
  3. Now come to the code. You need to create a custom save action to achieve this. Have a look at my code.
using System;
using System.Collections.Generic;
using System.Net;
using System.Web;
using Sitecore.Data;
using Sitecore.Diagnostics;
using Sitecore.WFFM.Abstractions.Actions;
using Sitecore.WFFM.Abstractions.Shared;
using Sitecore.WFFM.Actions.SaveActions;
using Sitecore.Data.Items;
using Sitecore;

namespace SitecoreTricks
{
 public class DownloadMediaOnSuccess : SendMessage
 {
 public DownloadMediaOnSuccess (ISettings settings, IMailSender mailSender) : base(settings, mailSender)
 {}

public override void Execute(ID formId, AdaptedResultList adaptedFields,
 ActionCallContext actionCallContext = null,
 params object[] data)
 {

 var formData = new Dictionary<string, string>();
 foreach (AdaptedControlResult acr in adaptedFields)
 {
 formData.Add(acr.FieldName, acr.Value);
 }
var MediaItemId = !string.IsNullOrEmpty(formData["FileToDownload"])
? formData["FileToDownload"]
: string.Empty;

var item = Context.Database.GetItem(new ID(MediaItemId));
 var mediaItem = (MediaItem) item;

HttpContext.Current.Response.Clear();
 HttpContext.Current.Response.ContentType = mediaItem.MimeType;
 HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Format("inline;filename=\"{0}\"", mediaItem.Name+"."+mediaItem.Extension));
 HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.OK;
 HttpContext.Current.Response.BufferOutput = true;
 mediaItem.GetMediaStream().CopyTo(HttpContext.Current.Response.OutputStream);
 HttpContext.Current.Response.Flush();
 HttpContext.Current.Response.End();

} 
 }
 }

That’s it. The PDF will be opened in the browser from there you can download it.

Reference: Learn how to hide a field on a WFFM form

Hide a field on WFFM (No CSS & JS)

WFFM provides an inbuilt way to hide one or more fields form rendered for an end user. Of course, I am not gonna use any CSS or JS to achieve this, instead, I’ll show you to do this by using an inbuilt feature of WFFM.

  1. Go to Form Designer and select the field you want to hide and scroll the left bar and click on Edit button shown under the Rules section.

WFFM1

2. Clicking on Edit button will open a popup with some predefined conditions and actions. Search for the “Database” and select the “where the current database name compares to value”. Click on “compare to select the compare rule and from there select “Is Equal To” and then click on “Value” and enter your web database connection string name like “web”.

WFFM-Edit-Rule-1

3. Now select the “hide element” actions tab and click ok.

WFFM-Edit-Rule-2

that’s it. Save the changes and publish the form. Now, the selected filed will not be available in the browser.