Sunday, May 31, 2015

Saving Form data with the Web Forms For Marketers MVC Form API

As many people have noticed with the Web Forms For Marketers MVC Forms the data does not serialize correctly so using custom save actions you cannot get the data from the form correctly.

There are a few rather complex workarounds for this mostly involving parsing the form from the form values in the http response.

The easiest solution to this issue is to create your own Form Processor which you can inject with a configuration file.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <wffm>
      <formProcessors>
        <processor type="MyNamespace.Forms.MVC.Processors.FormProcessor, MyNamespace.Forms.MVC" />
      </formProcessors>
    </wffm>
  </sitecore>
</configuration>

The processor itself is actually quite easy to write you simply need a class that inherits Sitecore.Forms.MVC.Processors.FormProcessor, then you can implement your save action in the FormSuccessValidation event.

namespace MyNamespace.Forms.MVC.Processors
{
    public class FormProcessor : FormProcessor
    {
        public FormProcessor()
        {
            SuccessValidation += FormSuccessValidation;
        }
 
        private void SaveForm(FormModel form)
        {
            var Fields = form.Sections.SelectMany(i => i.Fields);
        }
 
        public void FormSuccessValidation(object source, FormEventArgs args)
        {
            SaveForm(args.Form);
        }
    }
}

You can see from my code that you can access the fields through the given model in the form.Sections property.  

I find that this is much easier to implement than a custom save action (though it will not show up in the drop down list which may be a problem for some people, you can use this to store the data to access in a custom save action).  

If you have any questions related to Web Forms For Marketers please don't hesitate to ask as I have spent a lot of time in that code lately.

Error 500.19 Configuration File Too Long Sitecore Web.config

It is quite well known that the web.config file in Sitecore is monstrous.  It takes very little to make it large enough to get the 500.19 .NET error with the file being too large.  Most resolutions to this involve modifying the registry, but it does not really have to be that difficult.

The easiest solution is to take the Sitecore node out of the file and put it in a separate file similar to how the connection strings are pulled out.

The easiest way to get the whole node selected would be to use a text editor that supports regular expressions and use the expression (<sitecore)[\s\S]*(</sitecore>) to select it.  Then you simply cut it and past it into a file (I generally put it in a file called Sitecore.config in the App_Config folder).

After this you need to put an empty Sitecore node where you removed the old one from and add a configsource attribute that points to the new config file.

<sitecore configSource="App_Config/Sitecore.config" />