NomadTraveler
NomadTraveler

Reputation: 1084

WFFM 8 - submit data programmatically

I am on WFFM 8 rev. 150625

I have created a form that would submit data to WFFM programmatically.

The code snipped to submit data is:

var simpleForm = new SitecoreSimpleForm(sitecoreFormItem);
var actionList = simpleForm.FormItem.ActionsDefinition;

var actionDefinitions = new List<ActionDefinition>();
actionDefinitions.AddRange(actionList.Groups.SelectMany(x => x.ListItems)
                 .Select(li => new ActionDefinition(li.ItemID, li.Parameters)  
                 { UniqueKey = li.Unicid }));

//crList is ControlResult[] and contains field values. 
SubmitActionManager.Execute(sitecoreFormItem.ID, 
                            crList.ToArray(), actionDefinitions.ToArray());

My WFFM form in sitecore has no save action, as I am sending out the email in the code itself. I noticed that the data was being saved in MongoDB, but not in the reporting database.

Is there any way I can trigger the save to reporting DB action? Do I need to call some other function to execute that bit?

Upvotes: 0

Views: 872

Answers (2)

Jack Spektor
Jack Spektor

Reputation: 1117

Try this code. It will work for Sitecore 8.0 and higher. Note that this code doesn't trigger the save actions. If you want to trigger them you'll need to manually pass them into the FormDataHandler.ProcessData

 var controlResults = new List<ControlResult>();
                    controlResults.Add(new ControlResult(Pdf_Request_Form.Name.ItemID.ToString(), "Name", name, string.Empty));
                    controlResults.Add(new ControlResult(Pdf_Request_Form.Email.ItemID.ToString(), "Email", email, string.Empty));

    #pragma warning disable 618
                    FormDataHandler.ProcessData(Pdf_Request_Form.ItemID, controlResults.ToArray(), new IActionDefinition[] {}, DependenciesManager.ActionExecutor);
    #pragma warning restore 618

Upvotes: 0

Jonathan Robbins
Jonathan Robbins

Reputation: 2047

It should be possible. Essentially you need create a new Instance of the SaveToDatabase in your code. Then you need Execute the Save Action passing in the FormId, AdaptedResultList, containing the Fields and the Values.

    public void SubmitToDatabase(FormItem formItem, string[] values)
    {
        // Create AdaptedControlResult for the Fields of the form 
        // even better if you can pass the AdaptedResultList
        // from the form directly
        var adaptedControlResults = new List<AdaptedControlResult>();
        foreach (FieldItem fieldItem in formItem.FieldItems.ToList())
        {
            adaptedControlResults.Add(new AdaptedControlResult(new ControlResult(fieldItem.Name, values[formItem.FieldItems.ToList().IndexOf(fieldItem)], null), true));
        }

        var adaptedResultList = new AdaptedResultList(adaptedControlResults);

        Sitecore.Form.Submit.SaveToDatabase saveToDatabaseSaveAction = new Sitecore.Form.Submit.SaveToDatabase();
        try
        {
            saveToDatabaseSaveAction.Execute(formItem.ID, adaptedResultList, null);
        }
        catch (Exception ex)
        {
            Sitecore.Diagnostics.Log.Error(ex.Message, ex, this);
        }

The most complex bit will be passing the values the user has entered to this method to save. I'd recommend passing the AdaptedResultList directly where possible.

Reference - http://mikerobbins.co.uk/2014/10/15/write-to-web-forms-programmatically/

Upvotes: 0

Related Questions