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
ResourceInfoProvider.SetResourceInfo(newModule);


> Back to list of examples

Updating a module




// Gets the module
ResourceInfo updateModule = ResourceInfoProvider.GetResourceInfo("Custom.Module");
if (updateModule != null)
{
    // Updates the module properties
    updateModule.ResourceDisplayName = updateModule.ResourceDisplayName.ToLower();

    // Saves the changes to the database
    ResourceInfoProvider.SetResourceInfo(updateModule);
}


> Back to list of examples

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
    ResourceInfoProvider.SetResourceInfo(module);
}


> Back to list of examples

Assigning a module to a site




// Gets the module
ResourceInfo module = ResourceInfoProvider.GetResourceInfo("Custom.Module");
if (module != null)
{
    // Assigns the module to the current site
    ResourceSiteInfoProvider.AddResourceToSite(module.ResourceID, SiteContext.CurrentSiteID);
}


> Back to list of examples

Removing a module from a site




// Gets the module
ResourceInfo module = ResourceInfoProvider.GetResourceInfo("Custom.Module");
if (module != null)
{
    // Gets the binding object representing the relationship between the module and the current site
    ResourceSiteInfo moduleSite = ResourceSiteInfoProvider.GetResourceSiteInfo(module.ResourceID, SiteContext.CurrentSiteID);

    if (moduleSite != null)
    {
        // Removes the module from the current site
        ResourceSiteInfoProvider.DeleteResourceSiteInfo(moduleSite);
    }
}


> Back to list of examples

Deleting a module




// Gets the module
ResourceInfo deleteModule = ResourceInfoProvider.GetResourceInfo("Custom.Module");

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


> Back to list of examples

Module permissions

Defining a new permission for a module




// Gets the module
ResourceInfo module = ResourceInfoProvider.GetResourceInfo("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
    PermissionNameInfoProvider.SetPermissionInfo(newPermission);
}


> Back to list of examples

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
    PermissionNameInfoProvider.SetPermissionInfo(updatePermission);
}


> Back to list of examples

Updating multiple module permissions




// Gets a module
ResourceInfo module = ResourceInfoProvider.GetResourceInfo("Custom.Module");

if (module != null)
{
    // Gets all permissions of the specified module
    var permissions = PermissionNameInfoProvider.GetPermissionNames().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
        PermissionNameInfoProvider.SetPermissionInfo(permission);
    }
}


> Back to list of examples

Assigning a module permission to a role




// Gets the module permission
PermissionNameInfo permission = PermissionNameInfoProvider.GetPermissionNameInfo("Read", "Custom.Module", null);

// Gets the role
RoleInfo role = RoleInfoProvider.GetRoleInfo("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
    RolePermissionInfoProvider.SetRolePermissionInfo(newRolePermission);
}


> Back to list of examples

Removing a module permission from a role




// Gets the module permission
PermissionNameInfo permission = PermissionNameInfoProvider.GetPermissionNameInfo("Read", "Custom.Module", null);

// Gets the role
RoleInfo role = RoleInfoProvider.GetRoleInfo("Admin", SiteContext.CurrentSiteID);

if ((permission != null) && (role != null))
{
    // Gets the object representing the role-permission relationship
    RolePermissionInfo deleteRolePermission = RolePermissionInfoProvider.GetRolePermissionInfo(role.RoleID, permission.PermissionId);

    if (deleteRolePermission != null)
    {
        // Removes the permission from the role
        RolePermissionInfoProvider.DeleteRolePermissionInfo(deleteRolePermission);
    }
}


> Back to list of examples

Deleting a module permission




// Gets the module permission
PermissionNameInfo deletePermission = PermissionNameInfoProvider.GetPermissionNameInfo("Read", "Custom.Module", null);

if (deletePermission != null)
{
    // Deletes the permission
    PermissionNameInfoProvider.DeletePermissionInfo(deletePermission);
}


> Back to list of examples

UI elements

Creating a new UI element




// Gets the module to which the UI element will belong
ResourceInfo module = ResourceInfoProvider.GetResourceInfo("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
        UIElementInfoProvider.SetUIElementInfo(newElement);
    }
}


> Back to list of examples

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
    UIElementInfoProvider.SetUIElementInfo(updateElement);
}


> Back to list of examples

Updating multiple UI elements




// Gets a module
ResourceInfo module = ResourceInfoProvider.GetResourceInfo("Custom.Module");

if (module != null)
{
    // Gets all UI elements that belong to the specified module
    var elements = UIElementInfoProvider.GetUIElements().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
        UIElementInfoProvider.SetUIElementInfo(element);
    }
}


> Back to list of examples

Assigning a UI element to a role (UI personalization)




// Gets the role
RoleInfo role = RoleInfoProvider.GetRoleInfo("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
    RoleUIElementInfoProvider.SetRoleUIElementInfo(newRoleElement);
}


> Back to list of examples

Removing a UI element from a role (UI personalization)




// Gets the role
RoleInfo role = RoleInfoProvider.GetRoleInfo("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 = RoleUIElementInfoProvider.GetRoleUIElementInfo(role.RoleID, element.ElementID);

    if (deleteRoleElement != null)
    {
        // Removes the UI element from the role
        RoleUIElementInfoProvider.DeleteRoleUIElementInfo(deleteRoleElement);
    }
}


> Back to list of examples

Deleting a UI element




// Gets the UI element
UIElementInfo deleteElement = UIElementInfoProvider.GetUIElementInfo("Custom.Module", "CustomElement");

if (deleteElement != null)
{
    // Deletes the UI element
    UIElementInfoProvider.DeleteUIElementInfo(deleteElement);
}


> Back to list of examples