1

How can we add RoleToTheRole. For example i want to add ('sitecore\developer') role to the role ('sitecore\test') using power shell script.

In c# with the below code i am able to add role to the role

enter image description here

I tried adding member using like below, with the below syntax i am able to add the role in Members but my requirement is add the role in "Member Of" instead of "Members"

PS master:> Add-RoleMember -Identity 'Test' -Members "developer"

enter image description here

enter image description here

Please help me in fixing this issue.

Thank you in advance.

1

In C#

enter image description here

Sitecore Powershell script

$rolesarray = [System.Web.Security.Roles]::GetAllRoles()
[Collections.Generic.List[String]]$roles = $rolesarray
$indexOptions = [ordered]@{}
foreach($role in $roles) {
    $indexOptions[$role] = $role
}
$role=$indexOptions[0];
$mrole=$indexOptions[0];
$dialogProps = @{
    Parameters = @(

        @{ Name = "parentRole"; Value=$role; Title="Parent role"; Tooltip="List of roles"; Options=$indexOptions; Columns=6;},
        @{ Name = "memberRole"; Value=$mrole; Title="Member role"; Tooltip="List of roles"; Options=$indexOptions; Columns=6;}

    )
    Description = "Add the roles to role"
    Title = "Add the roles to role"
    Width = 700
    Height = 575
    OkButtonName = "Proceed"
    CancelButtonName = "Abort"
    ShowHint = $true
    Icon = [regex]::Replace($PSScript.Appearance.Icon, "Office", "OfficeWhite", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
}
$result = Read-Variable @dialogProps

if($result -ne "ok") {
    Exit
}

if($parentRole -eq $memberRole) {
    Show-Alert -Title "Both the role should not be same!"
}
else
{
if(([Sitecore.Security.Accounts.RolesInRolesManager]::RolesInRolesSupported) -And (-Not([Sitecore.Security.Accounts.RolesInRolesManager]::IsRoleInRole([Sitecore.Security.Accounts.Role]::FromName($memberRole),[Sitecore.Security.Accounts.Role]::FromName($parentRole),$false)) ))
{
[Sitecore.Security.Accounts.RolesInRolesManager]::AddRoleToRole([Sitecore.Security.Accounts.Role]::FromName($memberRole),[Sitecore.Security.Accounts.Role]::FromName($parentRole));
Show-Alert -Title "Successfully added"
}
else
{
Show-Alert -Title "Not supported role"
}
}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.