|
||
|
API examples for newer versions Please visit the latest API Examples documentation to view API examples for newer versions of Kentico. |
Use the following example to create a custom table item.
private bool CreateCustomTableItem() { // Creates new Custom table item provider CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
// Prepares the parameters string customTableClassName = "customtable.sampletable";
// Checks if Custom table 'Sample table' exists DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName); if (customTable != null) { // Creates new custom table item CustomTableItem newCustomTableItem = CustomTableItem.New(customTableClassName, customTableProvider);
// Sets the ItemText field value newCustomTableItem.SetValue("ItemText", "New text");
// Inserts the custom table item into database newCustomTableItem.Insert();
return true; }
return false; } |
Using the following example, you can get and update the custom table item that you created in the previous code example.
private bool GetAndUpdateCustomTableItem() { // Creates a new Custom table item provider CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string customTableClassName = "customtable.sampletable";
// Checks if Custom table 'Sample table' exists DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName); if (customTable != null) { // Prepares the parameters string where = "ItemText LIKE N'New text'"; int topN = 1; string columns = "ItemID";
// Gets the data set according to the parameters DataSet dataSet = customTableProvider.GetItems(customTableClassName, where, null, topN, columns);
if (!DataHelper.DataSourceIsEmpty(dataSet)) { // Gets the custom table item ID int itemID = ValidationHelper.GetInteger(dataSet.Tables[0].Rows[0][0], 0);
// Gets the custom table item CustomTableItem updateCustomTableItem = customTableProvider.GetItem(itemID, customTableClassName);
if (updateCustomTableItem != null) { string itemText = ValidationHelper.GetString(updateCustomTableItem.GetValue("ItemText"), "");
// Sets new values updateCustomTableItem.SetValue("ItemText", itemText.ToLowerCSafe());
// Saves the changes updateCustomTableItem.Update();
return true; } } }
return false; } |
Use this example to get a custom table item and move it down in the order of items in the custom table.
private bool GetAndMoveCustomTableItemDown() { // Creates new Custom table item provider CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string customTableClassName = "customtable.sampletable";
// Checks if Custom table 'Sample table' exists DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName); if (customTable != null) { // Prepares the parameters string where = "ItemText LIKE N'New text'"; int topN = 1; string columns = "ItemID";
// Gets the data set according to the parameters DataSet dataSet = customTableProvider.GetItems(customTableClassName, where, null, topN, columns);
if (!DataHelper.DataSourceIsEmpty(dataSet)) { // Gets the custom table item ID int itemID = ValidationHelper.GetInteger(dataSet.Tables[0].Rows[0][0], 0);
// Moves the item down customTableProvider.MoveItemDown(itemID, customTableClassName);
return true; } }
return false; } |
Use this example to get a custom table item and move it up in the order of items in the custom table.
private bool GetAndMoveCustomTableItemUp() { // Creates new Custom table item provider CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string customTableClassName = "customtable.sampletable";
// Checks if Custom table 'Sample table' exists DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName); if (customTable != null) { // Prepares the parameters string where = "ItemText LIKE N'New text'"; int topN = 1; string columns = "ItemID";
// Gets the data set according to the parameters DataSet dataSet = customTableProvider.GetItems(customTableClassName, where, null, topN, columns);
if (!DataHelper.DataSourceIsEmpty(dataSet)) { // Gets the custom table item ID int itemID = ValidationHelper.GetInteger(dataSet.Tables[0].Rows[0][0], 0);
// Moves the item up customTableProvider.MoveItemUp(itemID, customTableClassName);
return true; } }
return false; } |
Using the following example, you can get multiple custom table items based on a WHERE condition and then update the values in their ItemText columns.
private bool GetAndBulkUpdateCustomTableItems() { // Creates new Custom table item provider CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string customTableClassName = "customtable.sampletable";
// Checks if Custom table 'Sample table' exists DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName); if (customTable != null) { // Prepares the parameters
string where = "ItemText LIKE N'New text%'";
// Gets the data DataSet customTableItems = customTableProvider.GetItems(customTableClassName, where, null); if (!DataHelper.DataSourceIsEmpty(customTableItems)) { // Loops through the individual items foreach (DataRow customTableItemDr in customTableItems.Tables[0].Rows) { // Creates object from DataRow CustomTableItem modifyCustomTableItem = CustomTableItem.New(customTableItemDr, customTableClassName);
string itemText = ValidationHelper.GetString(modifyCustomTableItem.GetValue("ItemText"), "");
// Sets new values modifyCustomTableItem.SetValue("ItemText", itemText.ToUpper());
// Saves the changes modifyCustomTableItem.Update(); }
return true; } }
return false; } |
Use this example to delete the custom table item that you created in the first code example.
private bool DeleteCustomTableItem() { // Creates new Custom table item provider CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string customTableClassName = "customtable.sampletable";
// Checks if Custom table 'Sample table' exists DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName); if (customTable != null) { // Prepares the parameters string where = "ItemText LIKE N'New text%'";
// Gets the data DataSet customTableItems = customTableProvider.GetItems(customTableClassName, where, null); if (!DataHelper.DataSourceIsEmpty(customTableItems)) { // Loops through the individual items foreach (DataRow customTableItemDr in customTableItems.Tables[0].Rows) { // Creates object from DataRow CustomTableItem deleteCustomTableItem = CustomTableItem.New(customTableItemDr, customTableClassName);
// Deletes custom table item from database deleteCustomTableItem.Delete(); }
return true; } }
return false; } |