The following code example consists of a sequence of custom table data item management operations. A data item is first created and inserted into a custom table. After that, one of its columns is modified with different data. Next, the order of the item is modified. Finally, the item is deleted from the table and in the very end, all items in the custom table are deleted as well.
[C#]
using System; using System.Data; using CMS.SiteProvider; using CMS.CMSHelper; using CMS.GlobalHelper;
...
string tableName = "customtable.sampletable";
// Create an instance of the Custom table item provider CustomTableItemProvider tableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
// Create a custom table item object CustomTableItem cti = new CustomTableItem(tableName, tableProvider); // Set the ItemText field value cti.SetValue("ItemText", "Sample text"); // Insert the item object into the database cti.Insert();
// Get an item from a specified custom table cti = tableProvider.GetItem(1, tableName);
if (cti != null) { // Get names of the custom table columns string[] columns = cti.ColumnNames;
// If the table contains the ItemText column, get its value if (cti.ContainsColumn("ItemText")) { string text = ValidationHelper.GetString(cti.GetValue("ItemText"), "");
// Modify the column value text += " modified"; cti.SetValue("ItemText", text);
// Update the database record cti.Update(); }
// Check if the custom table items can be ordered if (cti.OrderEnabled) { // Move the item down tableProvider.MoveItemDown(cti.ItemID, tableName);
// Move the item up tableProvider.MoveItemUp(cti.ItemID, tableName); }
// Delete the custom table item from the database cti.Delete();
}
// Delete all items from a specified custom table tableProvider.DeleteItems(tableName); |
Page url: http://devnet.kentico.com/docs/5_5r2/devguide/index.html?api_custom_tables_complete_example.htm