using System;
using System.Data;
using CMS.CMSHelper;
using CMS.GlobalHelper;
using CMS.SettingsProvider;
using CMS.SiteProvider;
using CMS.UIControls;
[Title(Text = "Custom tables", ImageUrl = "Objects/CMS_CustomTable/object.png")]
public partial class CMSAPIExamples_Code_Development_CustomTables_Default : CMSAPIExamplePage
{
#region "Initialization"
protected void Page_Load(object sender, EventArgs e)
{
// CustomTableItem
apiCreateCustomTableItem.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(CreateCustomTableItem);
apiGetAndUpdateCustomTableItem.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndUpdateCustomTableItem);
apiGetAndMoveCustomTableItemDown.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndMoveCustomTableItemDown);
apiGetAndMoveCustomTableItemUp.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndMoveCustomTableItemUp);
apiGetAndBulkUpdateCustomTableItems.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndBulkUpdateCustomTableItems);
apiDeleteCustomTableItem.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(DeleteCustomTableItem);
}
#endregion
#region "Mass actions"
///
/// Runs all creating and managing examples.
///
public override void RunAll()
{
base.RunAll();
// Custom table item
apiCreateCustomTableItem.Run();
apiGetAndUpdateCustomTableItem.Run();
apiGetAndMoveCustomTableItemDown.Run();
apiGetAndMoveCustomTableItemUp.Run();
apiGetAndBulkUpdateCustomTableItems.Run();
}
///
/// Runs all cleanup examples.
///
public override void CleanUpAll()
{
base.CleanUpAll();
// Custom table item
apiDeleteCustomTableItem.Run();
}
#endregion
#region "API examples - CustomTableItem"
///
/// Creates custom table item. Called when the "Create item" button is pressed.
///
private bool CreateCustomTableItem()
{
// Create new Custom table item provider
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
// Prepare the parameters
string customTableClassName = "customtable.sampletable";
// Check if Custom table 'Sample table' exists
DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName);
if (customTable != null)
{
// Create new custom table item
CustomTableItem newCustomTableItem = CustomTableItem.New(customTableClassName, customTableProvider);
// Set the ItemText field value
newCustomTableItem.SetValue("ItemText", "New text");
// Insert the custom table item into database
newCustomTableItem.Insert();
return true;
}
return false;
}
///
/// Gets and updates custom table item. Called when the "Get and update item" button is pressed.
/// Expects the CreateCustomTableItem method to be run first.
///
private bool GetAndUpdateCustomTableItem()
{
// Create new Custom table item provider
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string customTableClassName = "customtable.sampletable";
// Check if Custom table 'Sample table' exists
DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName);
if (customTable != null)
{
// Prepare the parameters
string where = "ItemText LIKE N'New text'";
int topN = 1;
string columns = "ItemID";
// Get the data set according to the parameters
DataSet dataSet = customTableProvider.GetItems(customTableClassName, where, null, topN, columns);
if (!DataHelper.DataSourceIsEmpty(dataSet))
{
// Get the custom table item ID
int itemID = ValidationHelper.GetInteger(dataSet.Tables[0].Rows[0][0], 0);
// Get the custom table item
CustomTableItem updateCustomTableItem = customTableProvider.GetItem(itemID, customTableClassName);
if (updateCustomTableItem != null)
{
string itemText = ValidationHelper.GetString(updateCustomTableItem.GetValue("ItemText"), "");
// Set new values
updateCustomTableItem.SetValue("ItemText", itemText.ToLowerCSafe());
// Save the changes
updateCustomTableItem.Update();
return true;
}
}
}
return false;
}
///
/// Gets the custom table item and moves it down. Called when the "Get and move item down" button is pressed.
/// Expects the CreateCustomTableItem method to be run first.
///
private bool GetAndMoveCustomTableItemDown()
{
// Create new Custom table item provider
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string customTableClassName = "customtable.sampletable";
// Check if Custom table 'Sample table' exists
DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName);
if (customTable != null)
{
// Prepare the parameters
string where = "ItemText LIKE N'New text'";
int topN = 1;
string columns = "ItemID";
// Get the data set according to the parameters
DataSet dataSet = customTableProvider.GetItems(customTableClassName, where, null, topN, columns);
if (!DataHelper.DataSourceIsEmpty(dataSet))
{
// Get the custom table item ID
int itemID = ValidationHelper.GetInteger(dataSet.Tables[0].Rows[0][0], 0);
// Move the item down
customTableProvider.MoveItemDown(itemID, customTableClassName);
return true;
}
}
return false;
}
///
/// Gets the custom table item and moves it up. Called when the "Get and move item up" button is pressed.
/// Expects the CreateCustomTableItem method to be run first.
///
private bool GetAndMoveCustomTableItemUp()
{
// Create new Custom table item provider
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string customTableClassName = "customtable.sampletable";
// Check if Custom table 'Sample table' exists
DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName);
if (customTable != null)
{
// Prepare the parameters
string where = "ItemText LIKE N'New text'";
int topN = 1;
string columns = "ItemID";
// Get the data set according to the parameters
DataSet dataSet = customTableProvider.GetItems(customTableClassName, where, null, topN, columns);
if (!DataHelper.DataSourceIsEmpty(dataSet))
{
// Get the custom table item ID
int itemID = ValidationHelper.GetInteger(dataSet.Tables[0].Rows[0][0], 0);
// Move the item up
customTableProvider.MoveItemUp(itemID, customTableClassName);
return true;
}
}
return false;
}
///
/// Gets and bulk updates custom table items. Called when the "Get and bulk update items" button is pressed.
/// Expects the CreateCustomTableItem method to be run first.
///
private bool GetAndBulkUpdateCustomTableItems()
{
// Create new Custom table item provider
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string customTableClassName = "customtable.sampletable";
// Check if Custom table 'Sample table' exists
DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName);
if (customTable != null)
{
// Prepare the parameters
string where = "ItemText LIKE N'New text%'";
// Get the data
DataSet customTableItems = customTableProvider.GetItems(customTableClassName, where, null);
if (!DataHelper.DataSourceIsEmpty(customTableItems))
{
// Loop through the individual items
foreach (DataRow customTableItemDr in customTableItems.Tables[0].Rows)
{
// Create object from DataRow
CustomTableItem modifyCustomTableItem = CustomTableItem.New(customTableItemDr, customTableClassName);
string itemText = ValidationHelper.GetString(modifyCustomTableItem.GetValue("ItemText"), "");
// Set new values
modifyCustomTableItem.SetValue("ItemText", itemText.ToUpper());
// Save the changes
modifyCustomTableItem.Update();
}
return true;
}
}
return false;
}
///
/// Deletes customTableItem. Called when the "Delete item" button is pressed.
/// Expects the CreateCustomTableItem method to be run first.
///
private bool DeleteCustomTableItem()
{
// Create new Custom table item provider
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string customTableClassName = "customtable.sampletable";
// Check if Custom table 'Sample table' exists
DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName);
if (customTable != null)
{
// Prepare the parameters
string where = "ItemText LIKE N'New text%'";
// Get the data
DataSet customTableItems = customTableProvider.GetItems(customTableClassName, where, null);
if (!DataHelper.DataSourceIsEmpty(customTableItems))
{
// Loop through the individual items
foreach (DataRow customTableItemDr in customTableItems.Tables[0].Rows)
{
// Create object from DataRow
CustomTableItem deleteCustomTableItem = CustomTableItem.New(customTableItemDr, customTableClassName);
// Delete custom table item from database
deleteCustomTableItem.Delete();
}
return true;
}
}
return false;
}
#endregion
}