How do I save a form without using a submission button (I have another trigger on the page that can be used to run the saving code). The form is never shown to the user, I just need it to save to a web forms for marketers database, unless there is a smarter way of course.
I can get it out just fine using
Sitecore.Forms.Core.Data.FormItem form =
Sitecore.Forms.Core.Data.FormItem.GetForm(formID);
However, I want to
(i.e. something like)
form.Fields["Name"] = "Test name";
// this wont work, I dont know how to access the field's value before I store it.
After I have modified the fields, I would like to save it of course.
The way to do it was to first get a copy of the form, set its fields using a list of AdaptedControlResult and save it using InsertForm. This way it ends up in the WFFM's data table correctly.
The customerInfo used in the code, is from a CartHelper, but it could be anything.
Code:
string formID = "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}";
DataUri uri = new DataUri(new ID(formID));
Sitecore.Data.Items.Item formItem = Sitecore.Context.Database.GetItem(uri);
// if the item exists, run through all the known fields and add the values from the customerinfo into them.
if (formItem != null)
{
List<AdaptedControlResult> acrList = new List<AdaptedControlResult>();
Sitecore.Data.Items.Item[] fieldItems = formItem.Axes.GetDescendants();
foreach (Sitecore.Data.Items.Item fieldItem in fieldItems) //.Where(x => x.TemplateName == "Field"))
{
if (fieldItem.Name == "E-mail")
{
acrList.Add(new AdaptedControlResult(new ControlResult(fieldItem.Name, System.Web.HttpUtility.UrlDecode(customerInfo.Email), string.Empty) { FieldID = fieldItem.ID.ToString(), FieldName = fieldItem.Name, Value = System.Web.HttpUtility.UrlDecode(customerInfo.Email) }, true));
continue;
}
// same for all other fields.
}
// save data
if (acrList.Count > 0)
{
AdaptedResultList arl = new AdaptedResultList(acrList);
try
{
Sitecore.Forms.Data.DataManager.InsertForm(formItem.ID, arl, Sitecore.Data.ID.NewID, null);
}
catch (Exception ex)
{
// Log data here
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With