API examples

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

The following code examples show how you can handle custom tables data using Kentico CMS API.

 

Deleting an item from a custom table

 

[C#]

 

using CMS.SettingsProvider;

using CMS.SiteProvider;

using CMS.DataEngine;

using CMS.CMSHelper;

 

string customTableClassName = "custom.SampleTable";

 

// Get data class using custom table name

DataClassInfo customTableClassInfo = DataClassInfoProvider.GetDataClass(customTableClassName);

 

// Check if data class info exists

if (customTableClassInfo != null)

{

// Initialize custom table item provider with current user info and general connection

CustomTableItemProvider ctiProvider = new CustomTableItemProvider(CMSContext.CurrentUser, ConnectionHelper.GetConnection());

 

 // Provide ID of item you want to delete

 int itemId = 1;

 

// Get custom table item with given item ID

CustomTableItem item = ctiProvider.GetItem(itemId, customTableClassInfo.ClassName);

 

// Check if item exists

if (item != null)

{

// Delete item

item.Delete();

}

}

 

 

Adding an item into a custom table

 

[C#]

 

using CMS.SettingsProvider;

using CMS.SiteProvider;

using CMS.DataEngine;

using CMS.CMSHelper;

 

string customTableClassName = "custom.SampleTable";

 

// Get data class using custom table name

DataClassInfo customTableClassInfo = DataClassInfoProvider.GetDataClass(customTableClassName);

if (customTableClassInfo == null)

{

throw new Exception("Given custom table does not exist.");

}

 

// Initialize custom table item provider with current user info and general connection

CustomTableItemProvider ctiProvider = new CustomTableItemProvider(CMSContext.CurrentUser, ConnectionHelper.GetConnection());

 

// Create new custom table item for given class of custom table

CustomTableItem item = new CustomTableItem(customTableClassInfo.ClassName, ctiProvider);

 

// Set value of a custom table item field

item.SetValue("TestField", "Sample item");

 

// Insert the item

item.Insert();

 

 

Editing an item in a custom table

 

[C#]

 

using CMS.SettingsProvider;

using CMS.SiteProvider;

using CMS.DataEngine;

using CMS.CMSHelper;

 

string customTableClassName = "custom.SampleTable";

 

// Get data class using custom table name

DataClassInfo customTableClassInfo = DataClassInfoProvider.GetDataClass(customTableClassName);

if (customTableClassInfo == null)

{

throw new Exception("Given custom table does not exist.");

}

 

// Initialize custom table item provider with current user info and general connection

CustomTableItemProvider ctiProvider = new CustomTableItemProvider(CMSContext.CurrentUser, ConnectionHelper.GetConnection());

 

// Provide ID of item you want to edit

int itemId = 1;

 

// Get custom table item with given ID

CustomTableItem item = ctiProvider.GetItem(itemId, customTableClassInfo.ClassName);

 

// Set value of the custom table item field

item.SetValue("TestField", "Sample item");

 

// Update item

item.Update();

 

 

Getting items from a given custom table

 

[C#]

 

using CMS.SettingsProvider;

using CMS.SiteProvider;

using CMS.CMSHelper;

using CMS.DataEngine;

using CMS.GlobalHelper;

 

string customTableClassName = "custom.SampleTable";

 

// Get data class using custom table name

DataClassInfo customTableClassInfo = DataClassInfoProvider.GetDataClass(customTableClassName);

if (customTableClassInfo == null)

{

throw new Exception("Given custom table does not exist.");

}

 

// Initialize custom table item provider with current user info and general connection

CustomTableItemProvider ctiProvider = new CustomTableItemProvider(CMSContext.CurrentUser, ConnectionHelper.GetConnection());

 

// Get custom table items

DataSet dsItems = ctiProvider.GetItems(customTableClassInfo.ClassName, null, null);

 

// Check if DataSet is not empty

if (!DataHelper.DataSourceIsEmpty(dsItems))

{

// Handle the retrieved data

}

 

Page url: http://devnet.kentico.com/docs/devguide/index.html?custom_tables_api_examples.htm