Is this really the version you are looking for? Kentico Xperience 8.1 Documentation is no longer updated. Use the switcher in the header to choose your product version.
You can use the Kentico API to manage the data that users submit through forms. Two different approaches are available:
Use the following approach to access the data of a form at any time:
To get the values of specific fields from BizFormItem objects, call the GetValue methods — either the general GetValue, which returns an object, or methods for specific data types, such as GetStringValue.
To set values for fields, use the SetValue method for individual fields and then call SubmitChanges for the BizFormItem object to save the results.
using CMS.OnlineForms;
using CMS.DataEngine;
using CMS.SiteProvider;
using CMS.Helpers;
...
// Gets the form info object for the 'ContactUs' form
BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo("ContactUs", SiteContext.CurrentSiteID);
// Gets the class name of the 'ContactUs' form
DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
string className = formClass.ClassName;
// Loads the form's data
ObjectQuery<BizFormItem> data = BizFormItemProvider.GetItems(className);
// Checks whether the form contains any records
if (!DataHelper.DataSourceIsEmpty(data))
{
// Loops through the form's data records
foreach (BizFormItem item in data)
{
string firstNameFieldValue = item.GetStringValue("FirstName", "");
string lastNameFieldValue = item.GetStringValue("LastName", "");
// Perform any required logic with the form field values
// Variable representing a custom value that you want to save into the form data
object customFieldValue;
// Programatically assigns and saves a value for the form record's 'CustomField' field
item.SetValue("CustomField", customFieldValue);
item.SubmitChanges(false);
}
}To perform custom actions directly when form actions occur, implement handlers for BizFormItemEvents events.
Register the event handler at the beginning of the application's life cycle. Choose one of the following options:
To access the data of the active form in the event handler, use the Item property (BizFormItem type) of the BizFormItemEventArgs parameter. The parameter is available for all form item event handlers.
using CMS.Base;
using CMS.OnlineForms;
/// <summary>
/// Partial class that allows you to register custom handler methods and classes.
/// Adds the CustomFormHandlers attribute to the partial class.
/// </summary>
[CustomFormHandlers]
public partial class CMSModuleLoader
{
/// <summary>
/// Custom attribute class.
/// </summary>
private class CustomFormHandlers: CMSLoaderAttribute
{
/// <summary>
/// Called automatically when the application starts
/// </summary>
public override void Init()
{
// Assigns a handler to the BizFormItemEvents.Insert.After event
// This event occurs after the creation of every new form record
BizFormItemEvents.Insert.After += FormItem_InsertAfterHandler;
}
/// <summary>
/// Handles the form data when users create new records for the 'ContactUs' form
/// </summary>
private void FormItem_InsertAfterHandler(object sender, BizFormItemEventArgs e)
{
// Gets the form data object from the event handler parameter
BizFormItem formDataItem = e.Item;
// Checks that the form record was successfully created
// Ensures that the custom actions only occur for records of the 'ContactUs' form
// The values of form class names must be in lower case
if (formDataItem != null && formDataItem.BizFormClassName == "bizform.contactus")
{
string firstNameFieldValue = formDataItem.GetStringValue("FirstName", "");
string lastNameFieldValue = formDataItem.GetStringValue("LastName", "");
// Perform any required logic with the form field values
// Variable representing a custom value that you want to save into the form data
object customFieldValue;
// Programatically assigns and saves a value for the form record's 'CustomField' field
formDataItem.SetValue("CustomField", customFieldValue);
formDataItem.SubmitChanges(false);
}
}
}
}