Sample: Add, remove, or get a role from another role

Abstract

How to use the Sitecore.Security.Accounts.RolesInRoles.Manager class to operate with roles in roles.

This Sitecore.Security.Accounts.RolesInRolesManager class describes how .NET APIs operate with roles in roles.

You can add an existing role to another existing role using the Sitecore.Security.Accounts.RolesInRolesManager.AddRoleToRole (Role memberRoles, Role targetRoles) method. The first parameter is a member role and the second parameter is a target role.

To assign a role to another role:

const string parentRole = @"sitecore\Author";
const string memberRole = @"sitecore\MyRole";
if (RolesInRolesManager.RolesInRolesSupported && !RolesInRolesManager.IsRoleInRole(Role.FromName(memberRole), Role.FromName(parentRole), false))
{
   RolesInRolesManager.AddRoleToRole(Role.FromName(memberRole), Role.FromName(parentRole));
}

You can remove an existing role from another existing role using the Sitecore.Security.Accounts.RolesInRolesManager.RemoveRoleFromRole (Role memberRoles, Role targetRoles) method. The first parameter is a member role and the second parameter is a target role.

const string parentRole = @"sitecore\Author";
const string memberRole = @"sitecore\MyRole";
if (RolesInRolesManager.RolesInRolesSupported && RolesInRolesManager.IsRoleInRole(Role.FromName(memberRole), Role.FromName(parentRole), false))
{
    RolesInRolesManager.RemoveRoleFromRole(Role.FromName(memberRole), Role.FromName(parentRole));
}

You can get an existing role from another existing role using the Sitecore.Security.Accounts.RolesInRolesManager.GetRolesInRole (Role targetRole, bool includeIndirectMembership) method. The first parameter is a role and the second parameter determines whether you need indirect membership.

To get a role from another role:

const string parentRole = @"sitecore\Author";
IEnumerable<Role> roleList = RolesInRolesManager.GetRolesInRole(Role.FromName(parentRole), false);