Sample: Add, remove, or get a role from another role
This Sitecore.Security.Accounts.RolesInRolesManager class describes how .NET APIs operate with roles in roles.
Add a role to a role
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));
}Remove a role from a role
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));
}Get a role from a role
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);