Saving data to the Sitecore WFFM database programmatically

I am sure I am not the first to think that the Sitecore WFFM forms are not really as robust as we might like.
But because of the ready made form reports and marketing advantages, we might still want to leverage the wffm backend database for storing the form information, with an independent form front end.

This post is to show how we can save the form information to the wffm database without using the actions provided on submit.
Say you have your list of fields populated in a list of this class:

    public class WffmField
            public string FieldName { get; set; }
            public string FieldGuid { get; set; }
            public string FieldValue { get; set; }

The field guid would be the guid from sitecore:

You can then save to the WFFM database:

    // This should be populated with the data you want to send to the WFFM database
    var fields = new List<WffmField>(); 
    var wffmDatabaseFields = fields.Select(GetWFFMDatabaseField).ToList();
    	formId: new Sitecore.Data.ID("<Form guid here>"),
    	fields: new AdaptedResultList(wffmDatabaseFields),
    	sessionID: AnalyticsTracker.SessionId,
    	data: null);

This data will be available in the form reports now as expected.


Please note, you will need to add a reference to Sitecore.Forms.Core dll and following are the included namespaces:

using Sitecore.Form.Core.Analytics;
using Sitecore.Form.Core.Client.Data.Submit;
using Sitecore.Form.Core.Controls.Data;