Kentico CMS 6.0 Developer's Guide

Managing modules

Managing modules

Previous topic Next topic Mail us feedback on this topic!  

Managing modules

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 module.

 

private bool CreateModule()
{
  // Create new module object
  ResourceInfo newModule = new ResourceInfo();

 
  // Set the properties
   newModule.ResourceDisplayName = "My new module";
   newModule.ResourceName = "MyNewModule";

 
  // Save the module
  ResourceInfoProvider.SetResourceInfo(newModule);

 
  return true;
}

 

The following example gets and updates a module.

 

private bool GetAndUpdateModule()
{
  // Get the module
  ResourceInfo updateModule = ResourceInfoProvider.GetResourceInfo("MyNewModule");
  if (updateModule != null)
   {
      // Update the properties
       updateModule.ResourceDisplayName = updateModule.ResourceDisplayName.ToLower();

 
      // Save the changes
      ResourceInfoProvider.SetResourceInfo(updateModule);

 
      return true;
   }

 
  return false;
}

 

The following example gets and bulk updates modules.

 

private bool GetAndBulkUpdateModules()
{
  // Prepare the parameters
  string where = "ResourceName LIKE N'MyNewModule%'";

 
  // Get the data
  DataSet modules = ResourceInfoProvider.GetResources(where, null);
  if (!DataHelper.DataSourceIsEmpty(modules))
   {
      // Loop through the individual items
      foreach (DataRow moduleDr in modules.Tables[0].Rows)
       {
          // Create object from DataRow
          ResourceInfo modifyModule = new ResourceInfo(moduleDr);

 
          // Update the properties
           modifyModule.ResourceDisplayName = modifyModule.ResourceDisplayName.ToUpper();

 
          // Save the changes
          ResourceInfoProvider.SetResourceInfo(modifyModule);
       }

 
      return true;
   }

 
  return false;
}

 

The following example adds a module to site.

 

private bool AddModuleToSite()
{
  /// Get the module
  ResourceInfo module = ResourceInfoProvider.GetResourceInfo("MyNewModule");
  if (module != null)
   {
      int moduleId = module.ResourceId;
      int siteId = CMSContext.CurrentSiteID;

 
      // Save the binding
      ResourceSiteInfoProvider.AddResourceToSite(moduleId, siteId);

 
      return true;
   }

 
  return false;
}

 

The following example gets and bulk updates site modules.

 

private bool GetAndBulkUpdateSiteModules()
{
  int siteId = CMSContext.CurrentSiteID;
  string where = "ResourceName LIKE N'MyNewModule%'";

 
  // Get the data
  DataSet modules = ResourceInfoProvider.GetResources(where, null, 0, null, siteId);
  if (!DataHelper.DataSourceIsEmpty(modules))
   {
      // Loop through the individual items
      foreach (DataRow moduleDr in modules.Tables[0].Rows)
       {
          // Create object from DataRow
          ResourceInfo modifyModule = new ResourceInfo(moduleDr);

 
          // Update the properties
           modifyModule.ResourceDisplayName = modifyModule.ResourceDisplayName.ToLower();

 
          // Save the changes
          ResourceInfoProvider.SetResourceInfo(modifyModule);
       }

 
      return true;
   }

 
  return false;
}

 

The following example removes a module from site.

 

private bool RemoveModuleFromSite()
{
  // Get the module
  ResourceInfo removeModule = ResourceInfoProvider.GetResourceInfo("MyNewModule");
  if (removeModule != null)
   {
      int siteId = CMSContext.CurrentSiteID;

 
      // Get the binding
      ResourceSiteInfo moduleSite = ResourceSiteInfoProvider.GetResourceSiteInfo(removeModule.ResourceId, siteId);

 
      // Delete the binding
      ResourceSiteInfoProvider.DeleteResourceSiteInfo(moduleSite);

 
      return true;
   }

 
  return false;
}

 

The following example deletes a module.

 

private bool DeleteModule()
{
  // Get the module
  ResourceInfo deleteModule = ResourceInfoProvider.GetResourceInfo("MyNewModule");

 
  // Delete the module
  ResourceInfoProvider.DeleteResourceInfo(deleteModule);

 
  return (deleteModule != null);
}