Kentico CMS 6.0 Developer's Guide

Managing custom table data

Managing custom table data

Previous topic Next topic Mail us feedback on this topic!  

Managing custom table data

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

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example creates a custom table item.

 

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 = new CustomTableItem(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;

}

 

The following example gets and updates the custom table item created by the previous code example.

 

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.ToLower());

 

              // Save the changes

               updateCustomTableItem.Update();

 

              return true;

           }

       }

   }

 

  return false;

}

 

The following example gets a custom table item and moves it down in the order of items in the table.

 

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;

}

 

The following example gets a custom table item and moves it down in the order of items in the custom table.

 

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;

}

 

The following example gets multiple custom table items based on a WHERE condition and updates values in their ItemText columns.

 

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 = new CustomTableItem(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;

}

 

The following example deletes the custom table item created by the first code example on this page.

 

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 = new CustomTableItem(customTableItemDr, customTableClassName);

 

              // Delete custom table item from database

               deleteCustomTableItem.Delete();

           }

 

          return true;

       }

   }

 

  return false;

}