Modules
List of examples:
Modules
Creating a new module
// Creates a new module object
ResourceInfo newModule = new ResourceInfo();
// Sets the module properties
newModule.ResourceDisplayName = "Custom module";
newModule.ResourceName = "Custom.Module";
// Ensures that the module is in development mode (not sealed)
newModule.ResourceIsInDevelopment = true;
// Saves the module to the database
ResourceInfo.Provider.Set(newModule);
Updating a module
// Gets the module
ResourceInfo updateModule = ResourceInfo.Provider.Get("Custom.Module");
if (updateModule != null)
{
// Updates the module properties
updateModule.ResourceDisplayName = updateModule.ResourceDisplayName.ToLower();
// Saves the changes to the database
ResourceInfo.Provider.Set(updateModule);
}
Updating multiple modules
// Get all modules assigned to the current site whose code name has the 'Custom.' prefix
var modules = ResourceInfoProvider.GetResources(SiteContext.CurrentSiteID)
.WhereStartsWith("ResourceName", "Custom.");
// Loops through individual modules
foreach (ResourceInfo module in modules)
{
// Updates the module properties
module.ResourceDisplayName = module.ResourceDisplayName.ToUpper();
// Saves the changes to the database
ResourceInfo.Provider.Set(module);
}
Assigning a module to a site
// Gets the module
ResourceInfo module = ResourceInfo.Provider.Get("Custom.Module");
if (module != null)
{
// Assigns the module to the current site
ResourceSiteInfo.Provider.Add(module.ResourceID, SiteContext.CurrentSiteID);
}
Removing a module from a site
// Gets the module
ResourceInfo module = ResourceInfo.Provider.Get("Custom.Module");
if (module != null)
{
// Gets the binding object representing the relationship between the module and the current site
ResourceSiteInfo moduleSite = ResourceSiteInfo.Provider.Get(module.ResourceID, SiteContext.CurrentSiteID);
if (moduleSite != null)
{
// Removes the module from the current site
ResourceSiteInfo.Provider.Delete(moduleSite);
}
}
Deleting a module
// Gets the module
ResourceInfo deleteModule = ResourceInfo.Provider.Get("Custom.Module");
// Deletes the module
ResourceInfo.Provider.Delete(deleteModule);
Module permissions
Defining a new permission for a module
// Gets the module
ResourceInfo module = ResourceInfo.Provider.Get("Custom.Module");
if (module != null)
{
// Creates a new permission object
PermissionNameInfo newPermission = new PermissionNameInfo();
// Sets the permission properties
newPermission.PermissionDisplayName = "Read";
newPermission.PermissionName = "Read";
newPermission.ResourceId = module.ResourceID;
newPermission.PermissionDisplayInMatrix = true;
// Saves the permission to the database
PermissionNameInfo.Provider.Set(newPermission);
}
Updating a module permission
// Gets the module permission
PermissionNameInfo updatePermission = PermissionNameInfoProvider.GetPermissionNameInfo("Read", "Custom.Module", null);
if (updatePermission != null)
{
// Updates the permission properties
updatePermission.PermissionDisplayName = updatePermission.PermissionDisplayName.ToLower();
// Saves the changes to the database
PermissionNameInfo.Provider.Set(updatePermission);
}
Updating multiple module permissions
// Gets a module
ResourceInfo module = ResourceInfo.Provider.Get("Custom.Module");
if (module != null)
{
// Gets all permissions of the specified module
var permissions = PermissionNameInfo.Provider.Get().WhereEquals("ResourceId", module.ResourceID);
// Loops through the module's permissions
foreach (PermissionNameInfo permission in permissions)
{
// Updates the permission properties
permission.PermissionDisplayName = permission.PermissionDisplayName.ToUpper();
// Saves the changes to the database
PermissionNameInfo.Provider.Set(permission);
}
}
Assigning a module permission to a role
// Gets the module permission
PermissionNameInfo permission = PermissionNameInfoProvider.GetPermissionNameInfo("Read", "Custom.Module", null);
// Gets the role
RoleInfo role = RoleInfo.Provider.Get("Admin", SiteContext.CurrentSiteID);
if ((permission != null) && (role != null))
{
// Creates an object representing the role-permission relationship
RolePermissionInfo newRolePermission = new RolePermissionInfo();
// Assigns the permission to the role
newRolePermission.PermissionID = permission.PermissionId;
newRolePermission.RoleID = role.RoleID;
// Saves the role-permission relationship into the database
RolePermissionInfo.Provider.Set(newRolePermission);
}
Removing a module permission from a role
// Gets the module permission
PermissionNameInfo permission = PermissionNameInfoProvider.GetPermissionNameInfo("Read", "Custom.Module", null);
// Gets the role
RoleInfo role = RoleInfo.Provider.Get("Admin", SiteContext.CurrentSiteID);
if ((permission != null) && (role != null))
{
// Gets the object representing the role-permission relationship
RolePermissionInfo deleteRolePermission = RolePermissionInfo.Provider.Get(role.RoleID, permission.PermissionId);
if (deleteRolePermission != null)
{
// Removes the permission from the role
RolePermissionInfo.Provider.Delete(deleteRolePermission);
}
}
Deleting a module permission
// Gets the module permission
PermissionNameInfo deletePermission = PermissionNameInfoProvider.GetPermissionNameInfo("Read", "Custom.Module", null);
if (deletePermission != null)
{
// Deletes the permission
PermissionNameInfo.Provider.Delete(deletePermission);
}
UI elements
Creating a new UI element
// Gets the module to which the UI element will belong
ResourceInfo module = ResourceInfo.Provider.Get("Custom.Module");
if (module != null)
{
// Gets a parent UI element (the CMS > Administration > Custom element in this case)
UIElementInfo parentElement = UIElementInfoProvider.GetUIElementInfo("CMS", "Custom");
if (parentElement != null)
{
// Creates a new UI element object
UIElementInfo newElement = new UIElementInfo();
// Set the properties
newElement.ElementDisplayName = "Custom element";
newElement.ElementName = "CustomElement";
newElement.ElementResourceID = module.ResourceID;
newElement.ElementIsCustom = true;
newElement.ElementParentID = parentElement.ElementID;
// Saves the new UI element to the database
UIElementInfo.Provider.Set(newElement);
}
}
Updating a UI element
// Gets the UI element
UIElementInfo updateElement = UIElementInfoProvider.GetUIElementInfo("Custom.Module", "CustomElement");
if (updateElement != null)
{
// Updates the UI element properties
updateElement.ElementDisplayName = updateElement.ElementDisplayName.ToLower();
// Saves the changes to the database
UIElementInfo.Provider.Set(updateElement);
}
Updating multiple UI elements
// Gets a module
ResourceInfo module = ResourceInfo.Provider.Get("Custom.Module");
if (module != null)
{
// Gets all UI elements that belong to the specified module
var elements = UIElementInfo.Provider.Get().WhereEquals("ElementResourceID", module.ResourceID);
// Loops through the module's UI elements
foreach (UIElementInfo element in elements)
{
// Updates the UI element properties
element.ElementDisplayName = element.ElementDisplayName.ToUpper();
// Saves the changes to the database
UIElementInfo.Provider.Set(element);
}
}
Assigning a UI element to a role (UI personalization)
// Gets the role
RoleInfo role = RoleInfo.Provider.Get("Admin", SiteContext.CurrentSiteID);
// Gets the UI element
UIElementInfo element = UIElementInfoProvider.GetUIElementInfo("Custom.Module", "CustomElement");
if ((role != null) && (element != null))
{
// Creates an object representing the role-UI element relationship
RoleUIElementInfo newRoleElement = new RoleUIElementInfo();
// Assigns the UI element to the role
newRoleElement.RoleID = role.RoleID;
newRoleElement.ElementID = element.ElementID;
// Saves the new relationship to the database
RoleUIElementInfo.Provider.Set(newRoleElement);
}
Removing a UI element from a role (UI personalization)
// Gets the role
RoleInfo role = RoleInfo.Provider.Get("Admin", SiteContext.CurrentSiteID);
// Gets the UI element
UIElementInfo element = UIElementInfoProvider.GetUIElementInfo("Custom.Module", "CustomElement");
if ((role != null) && (element != null))
{
// Gets the object representing the relationship between the role and the UI element
RoleUIElementInfo deleteRoleElement = RoleUIElementInfo.Provider.Get(role.RoleID, element.ElementID);
if (deleteRoleElement != null)
{
// Removes the UI element from the role
RoleUIElementInfo.Provider.Delete(deleteRoleElement);
}
}
Deleting a UI element
// Gets the UI element
UIElementInfo deleteElement = UIElementInfoProvider.GetUIElementInfo("Custom.Module", "CustomElement");
if (deleteElement != null)
{
// Deletes the UI element
UIElementInfo.Provider.Delete(deleteElement);
}