8

I'm using Sitecore 8.2 Update 4 with the corresponding WFFM version.

Our marketing department is passing a tracking code from their various marketing channels onto one of our Sitecore Tenant sites. That code shows up in the URL as a query string parameter. I've got some JavaScript code inserted into the site that will strip the information from the site and put it into a cookie, because there are a lot of business rules about when to use that data and when not to. 

I've got a WFFM form that is used by the site visitors to send information to us. I need that form to send me that information so I can pass it on to the system of record. I currently have a custom save action that takes the data from the form and sends it where it needs to go.

What I need to know is how do I get the value stored in the cookie passed into my custom save action? Or is there some other way for me to get that information into my custom save action?

4 Answers 4

3

I'd create a custom field type. That would let you create a hidden input field on your form which you could populate with the value from the cookie. This way lets the value go through the whole process and get saved in the database rather than trying to append a value halfway through the save action pipeline.

3
  • 1
    I agree. I recently used this. Just make it pull from the cookie instead of Sitecore. jammykam.wordpress.com/2017/10/06/wffm-hidden-token-field
    – Chris Auer
    Jan 20, 2018 at 1:10
  • OK, so I've been able to follow the linked article, but I'm not sure where I need to put the Javascript to get the cookie. Does that happen in the .CSHTML file, or somewhere else? Or can I do something in the server-side code and get the cookie value that way? Jan 22, 2018 at 22:55
  • In the article, it looks like you'd put your server-side cookie logic in the GetTokenValue method to populate the value in the Initialize method. Jan 23, 2018 at 0:58
1

If I understand you correctly, you mention the value you needs to pass is come as query string parameters with the url. If that is the case, you can use that query string value and just populate WFFM field directly without any customizataion.

When you try to add a WFFM form using default WFFM "Form" webcontrol, you will see a checkbox with name "ReadQueryString" (refer below image).

If you have this checkbox is clicked, and if you pass any url parameters with a name of a fields in the WFFM form, sitecore will automatically populate that value

More info can be found in following blog post

https://sitecorefootsteps.blogspot.com/2015/04/how-to-populate-wffm-form-fields-using.html

1

You should have access to HttpContext in your save action.

It means that this code should work for you:

if (HttpContext.Current.Request.Cookies.AllKeys.Contains(key))
{
   value = HttpContext.Current.Request.Cookies[key].Value;
}

where key is string variable that equals to cookie key that you need, value is variable that will contain cookie value.

2
  • 3
    Be a little careful with this since HttpContext is only available when the Save Action is marked as a Client Action only (i.e. it runs on the CD server, not passed to the CM via Event Queue)
    – jammykam
    Jan 20, 2018 at 19:08
  • How can I access HttpContext in a save action? It is unavailable in mine...
    – Azimuth
    Apr 28, 2020 at 11:49
0

As I understood you need some value which is present in Querystring or Cookie. For this, you can apply the Rules but these are limited to QueryString and UserProfile. Here you can create new custom rule action and apply it with a suitable condition. Steps -

  1. Add a field and make it invisible ( with the use of CSS class or rule action ). Hide Field
  2. Apply custom rule that will read the value from source and populate the hidden field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.