Skip to content

Instantly share code, notes, and snippets.

@PetersonDave
Last active January 7, 2017 12:57
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 PetersonDave/5096452 to your computer and use it in GitHub Desktop.
Save PetersonDave/5096452 to your computer and use it in GitHub Desktop.
Web forms for marketers redirect assumes the success page is relative to the form item in the tree (template: /sitecore/templates/Web Forms for Marketers/Form). When this is not the case, wffm builds a redirect URL which is not valid. Since the success page is defined using a general link field, we don't have a Guid to lookup the actual success …
/*
Extends: Sitecore.Form.Core.Pipelines.FormSubmit
forms.config needs to be updated to:
<successAction>
<processor type="MyCustom.Pipelines.SuccessRedirect, MyLibrary"/>
<processor type="Sitecore.Form.Core.Pipelines.FormSubmit.FormatSuccessMessage, Sitecore.Forms.Core"/>
</successAction>
*/
public class SuccessRedirect : ClientPipelineArgs
{
public void Process(SubmitSuccessArgs args)
{
Assert.IsNotNull(args, "args");
if (args.Form == null)
{
Uri uri = null;
if (Uri.TryCreate(args.Result, UriKind.RelativeOrAbsolute, out uri))
{
bool isUriValid = uri != null && !string.IsNullOrEmpty(uri.PathAndQuery);
if (isUriValid)
{
args.AbortPipeline();
WebUtil.Redirect(args.Result, false);
}
}
}
else
{
if (args.Form.SuccessRedirect)
{
var url = GetRedirectUrl(args);
if (!string.IsNullOrEmpty(url))
{
args.AbortPipeline();
WebUtil.Redirect(url, false);
}
}
}
}
private string GetRedirectUrl(SubmitSuccessArgs args)
{
string url = string.Empty;
var path = "/sitecore/content" + args.Form.SuccessPage.Url; // don't love the hard-coded path here. Other ideas?
if (!string.IsNullOrEmpty(path))
{
var item = Sitecore.Context.Database.Items[path];
if (item != null)
{
url = LinkManager.GetItemUrl(item);
}
}
return url;
}
}
@scjunkie
Copy link

scjunkie commented Mar 6, 2013

This should remove the hard-coded path part on line 48:

var path = string.Concat(Sitecore.Context.Site.RootPath, args.Form.SuccessPage.Url);

@PetersonDave
Copy link
Author

@scjunkie I previously tried the code above, but got a path which was invalid. This is due to a specific configuration in a multi-site setup:

WFFM Form Page: sitecore/content/my-site/home/form
WFFM Form Success Page: sitecore/content/my-site/home/form/success
WFFM Form: /sitecore/content/my-site/forms/my-form

For the configuration above, the values map to:

Sitecore.Context.Site.RootPath: sitecore/content/my-site
args.Form.SuccessPage.Url: /my-site/home/form/success

Keep in mind this issue is specific to my configuration. While I get an invalid URL, others who have a different configuration will not get an invalid URL. The impact may be a URL, which works, but has the site home node in its URL.

@scjunkie
Copy link

scjunkie commented Mar 6, 2013

Yes -- if the rootPath attribute on your site node has a value different than /sitecore/content, then using Sitecore.Context.Site.RootPath won't work.

How about putting it in a patch include configuration file instead?

@PetersonDave
Copy link
Author

That's a good idea. I like it!

@jsabra1
Copy link

jsabra1 commented Jan 7, 2017

Hi @PetersonDave, Does this mean this is the reason for the reload of the form page that occurs on submit before actually forwarding the request to the success page?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment