This simple example below, is a condensed example which shows the submission of a form with a single field called Email. Essentially we get the actual form item to have access to it's fields and save actions. This is then used to create the parameters required to submit the form.
var myEmail = "ryan.bailey@email.com"; Sitecore.Forms.Core.Data.FormItem formItem = Sitecore.Forms.Core.Data.FormItem.GetForm(ID.Parse("{7D47FECC-25DF-4561-A911-683F4F164FA7}")); // Get the form item var emailField = formItem.Fields.Where(x => x.DisplayName == "Email").FirstOrDefault(); // Get a given field - should null check of course var controlResults = new List<ControlResult>(); controlResults.Add(new ControlResult(emailField.ID.ToString(), emailField.Name, myEmail ,emailField.Parameters,false)); // Parse save actions var saveActionXml = formItem.SaveActions; var actionList = Sitecore.Form.Core.ContentEditor.Data.ListDefinition.Parse(saveActionXml); var actionDefinitions = new List<ActionDefinition>(); actionDefinitions.AddRange(actionList.Groups.SelectMany(x => x.ListItems).Select(li => new ActionDefinition(li.ItemID, li.Parameters) { UniqueKey = li.Unicid })); Sitecore.Forms.Core.Handlers.FormDataHandler handler = Sitecore.WFFM.Abstractions.Dependencies.DependenciesManager.Resolve<Sitecore.Forms.Core.Handlers.FormDataHandler>(); handler.ProcessForm(ID.Parse("{7D47FECC-25DF-4561-A911-683F4F164FA7}"), controlResults.ToArray(), actionDefinitions.ToArray()); // Submit formThere are older examples available online, but this one works for me on 8.2 and the reporting is there as well.
No comments:
Post a Comment