Kentico CMS 7.0 Developer's Guide

Managing roles

Managing roles

Previous topic Next topic Mail us feedback on this topic!  

Managing roles

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

 

private void CreateRole()
{
    // Create new role object
    RoleInfo newRole = new RoleInfo();
 
    // Set the properties
    newRole.DisplayName = "My new role";
    newRole.RoleName = "MyNewRole";
    newRole.SiteID = CMSContext.CurrentSiteID;
 
    // Save the role
    RoleInfoProvider.SetRoleInfo(newRole);
}

 

The following example gets and updates a role.

 

private bool GetAndUpdateRole()
{

  // Get the role
  RoleInfo updateRole = RoleInfoProvider.GetRoleInfo("MyNewRole", CMSContext.CurrentSiteID);

    if (updateRole != null)
    {
        // Update the properties
        updateRole.DisplayName = updateRole.DisplayName.ToLower();
 
        // Save the changes
        RoleInfoProvider.SetRoleInfo(updateRole);
 
        return true;
    }
 
    return false;
}

 

The following example gets and bulk updates roles.

 

private bool GetAndBulkUpdateRoles()
{
    // Prepare the parameters
    string where = "RoleName LIKE N'MyNewRole%'";
 
    // Get the data
    DataSet roles = RoleInfoProvider.GetRoles(where, null);
    if (!DataHelper.DataSourceIsEmpty(roles))
    {
        // Loop through the individual items
        foreach (DataRow roleDr in roles.Tables[0].Rows)
        {
            // Create object from DataRow
            RoleInfo modifyRole = new RoleInfo(roleDr);
 
            // Update the properties
            modifyRole.DisplayName = modifyRole.DisplayName.ToUpper();
 
            // Save the changes
            RoleInfoProvider.SetRoleInfo(modifyRole);
        }
 
        return true;
    }
 
    return false;
}

 

The following example deletes a role.

 

private bool DeleteRole()
{

  // Get the role
  RoleInfo deleteRole = RoleInfoProvider.GetRoleInfo("MyNewRole", CMSContext.CurrentSiteID);

 
    // Delete the role
    RoleInfoProvider.DeleteRoleInfo(deleteRole);
 
    return (deleteRole != null);
}