Kentico CMS 7.0 Developer's Guide

Creating a new record

Creating a new record

Previous topic Next topic Mail us feedback on this topic!  

Creating a new record

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

The sample code below demonstrates how to add a new record to a form using the API.

 

[C#]

 

using CMS.SettingsProvider;

using CMS.FormEngine;

 

...
 

string bizFormName = "ContactUs";

string siteName = "CorporateSite";

 

// Read From definition

BizFormInfo bfi = BizFormInfoProvider.GetBizFormInfo(bizFormName, siteName);

 

if (bfi != null)

{

  // Read data type definition

  DataClassInfo dci = DataClassInfoProvider.GetDataClass(bfi.FormClassID);

 

  if (dci != null)

   {

      BizFormItemProvider bProvider = new BizFormItemProvider();

      // Create a new record in memory

      BizFormItem formRecord = BizFormItem.New(dci.ClassName, bProvider);

 

      // Insert some data

       formRecord.SetValue("FirstName", "Alice");

       formRecord.SetValue("LastName", "Cooper");

       formRecord.SetValue("Email", "alice@email.com");

       formRecord.SetValue("Message", "Hello world");

       formRecord.SetValue("PhoneNumber", "(123) 123-4567");

       formRecord.SetValue("FormInserted", DateTime.Now);

       formRecord.SetValue("FormUpdated", DateTime.Now);

 

      // Insert the new record in the database

       formRecord.Insert();

 

      // Update number of entries in BizFormInfo

      BizFormInfoProvider.RefreshDataCount(bfi.FormName, bfi.FormSiteID);

   }

}