Thursday, March 31, 2016

Sitecore 8.1 and WFFM and Form Data, Oh My

Sounds like something from Wizard of Oz, out of this world.  I love the new architecture for Webforms for Marketers.  I saw the presentation of it at SUGCON in NOLA and it wowed me.

Last night @adamborsi and I got to use it first hand for a client form.  We did run into an issue though. I wanted to put a summary of the results on the thank you page.  I came across an awesome article on how to access Form Data if you are using Sitecore 7.5 or earlier.

However if you are using Sitecore 8.1 then those methods are gone and you MUST use the new more robust provider.  I did some googling and came up with others asking "How do I create an AnalyticsFormsDataProvider"  it had a response so I was excited but unfortunately the response was "Were you able to find the answer to this?"  

Fortunately through a couple hours of trial and error and some googling of the interface names we were able to resolve it.  Here is the sample code:

var provider = new Sitecore.WFFM.Analytics.Providers.AnalyticsFormsDataProvider (new Sitecore.WFFM.Analytics.Dependencies.ReportDataProviderWrapper( Sitecore.WFFM.Abstractions.Dependencies.DependenciesManager.FactoryObjectsProvider), Sitecore.WFFM.Abstractions.Dependencies.DependenciesManager.Logger, Sitecore.WFFM.Abstractions.Dependencies.DependenciesManager.AnalyticsTracker, Sitecore.WFFM.Abstractions.Dependencies.DependenciesManager.Settings); 

foreach (var formData in provider.GetFormData(new Guid("{THIS_IS_YOUR_FORM_GUID}"))) 

   foreach (var field in formData.Fields) 
   { 
      // You can do what you want here with the field values 
      var fieldValue = field.Value; 
   } 
 }

If you are looking to export you can look at the original article and use that to export. For our purpose we were simply taking the value and summing it and dividing it by entries.

If you have any questions, tips, tricks or resources you would like to share with the guild email me at chris.williams@readwatchcreate.com  

Also, Adam Borsi would like to thank Mike Reynolds (SitecoreJunkie) for all the help he provides through his blogs and twitter.  Much appreciated.