jxycms
1/25/2017 - 5:48 AM

How to create a record in a Kentico BizForm through the Kentico API and customer table

How to create a record in a Kentico BizForm through the Kentico API and customer table

using System;
using CMS.OnlineForms;
using CMS.Helpers;
using CMS.SiteProvider;
using CMS.DataEngine;
using System.Data;
using CMS.CustomTables;

public partial class CMSPages_HSGAPostForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string address = ValidationHelper.GetString(Request.Form["address"], "");
        string interest_group = ValidationHelper.GetString(Request.Form["interest_group"], "");
        string interest = ValidationHelper.GetString(Request.Form["interest"], "");
        string name = ValidationHelper.GetString(Request.Form["name"], "");
        string email = ValidationHelper.GetString(Request.Form["email"], "");
        string phone = ValidationHelper.GetString(Request.Form["phone"], "");
        string region = ValidationHelper.GetString(Request.Form["region"], "");

        BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo("FindLocalOfficeForm", SiteContext.CurrentSiteID);

        if (formObject != null)
        {
            DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
            string formClassName = formClass.ClassName;

            BizFormItem newFormItem = BizFormItem.New(formClassName);
            string interestStr = string.IsNullOrEmpty(interest_group) ? interest : interest_group + " - " + interest;
            newFormItem.SetValue("Address_Postcode", address);
            newFormItem.SetValue("Interest", interestStr);
            newFormItem.SetValue("Name", name);
            newFormItem.SetValue("Email", email);
            newFormItem.SetValue("Phone", phone);
            newFormItem.Insert();

            string recipientEmailAddress = "";
            
            if (!String.IsNullOrEmpty(region))
            {
                DataSet emailDatabase = CustomTableItemProvider.GetItems("customtable.StateInterestEmailAddress")
                                .Column("email")
                                .WhereEquals("state", region)
                                .Result;
                
                recipientEmailAddress = emailDatabase.Tables[0].Rows[0]["email"].ToString();

            }
            if (!String.IsNullOrEmpty(recipientEmailAddress))
            {
                // get the content for the emails
                IDataClass content = DataClassFactory.NewDataClass(formClass.ClassName, newFormItem.ItemID);
                BizForm f = new BizForm();

                // check if notification
                if (!string.IsNullOrEmpty(formObject.FormSendFromEmail))
                {
                    f.SendNotificationEmail(
                        formObject.FormSendFromEmail,
                        recipientEmailAddress,
                        content, formObject);
                }
                
                if (!String.IsNullOrEmpty(formObject.FormConfirmationSendFromEmail))
                {
                    f.SendConfirmationEmail(
                        formObject.FormConfirmationSendFromEmail,
                        email,
                        content, formObject);
                }
            }
        }


    }
}