API examples

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

 

Deleting an item from a custom table

 

[C#]

 

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#]

 

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#]

 

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#]

 

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

}