How to create a record in a Kentico BizForm through the Kentico API

Tue Oct 15 2013

The following example shows how to create a record in a Kentico BizForm through the Kentico API.

Function that creates a record in a BizForm called "Form_Signup":

using CMS.FormEngine;
using CMS.CMSHelper;
using CMS.SettingsProvider;
using CMS.FormControls;
using CMS.DataEngine;

// function that saves the signup form to the database
public static int SaveSignupForm(SignupFormModel model)
{
    // get form definition
    BizFormInfo bfi = BizFormInfoProvider.GetBizFormInfo(
        "Form_Signup", CMSContext.CurrentSiteName);

    // check if valid
    if (bfi != null)
    {
        // get the type
        DataClassInfo dci = DataClassInfoProvider.GetDataClass(bfi.FormClassID);
        if (dci != null)
        {
            // get a provider
            BizFormItemProvider bProvider = new BizFormItemProvider();

            // create the record
            BizFormItem rec = BizFormItem.New(dci.ClassName, bProvider);

            // set the field value
            rec.SetValue("Email", model.Email);            rec.SetValue("FormInserted", DateTime.Now);
            rec.SetValue("FormUpdated", DateTime.Now);

            // insert
            rec.Insert();

            // update count
            BizFormInfoProvider.RefreshDataCount(bfi.FormName, bfi.FormSiteID);
            // return new rec id
            return rec.ItemID;
        }
    }
}

If you want to also send confirmation emails and/or notification emails, add the following to the end of the function:

// get the content for the emails
IDataClass content = DataClassFactory.NewDataClass(dci.ClassName, rec.ItemID);
BizForm f = new BizForm();

// check if confirmation
if (!String.IsNullOrEmpty(bfi.FormConfirmationSendFromEmail))
{    f.SendConfirmationEmail(
        bfi.FormConfirmationSendFromEmail,
        model.Email, 
        content, bfi);
}

// check if notification
if (!string.IsNullOrEmpty(bfi.FormSendFromEmail) && !string.IsNullOrEmpty(bfi.FormSendToEmail))
{
    f.SendNotificationEmail(
        bfi.FormSendFromEmail, 
        bfi.FormSendToEmail, 
        content, bfi);
}