3

I know it's possible to modify a library authorizations using the SharePoint UI following the steps bellow:

  1. Stop authorizations inheritance.

  2. Modify users authorizations.

  3. Select/Unselect the desired checkboxes.

  4. OK.

I want to add Reader authorization and remove Contributor.

How to achieve that using PowerShell?

So far, I succeeded to add new authorizations with PowerShell. Here is the code.

$web = Get-SPWeb "http://www.whatever";         
$list = $web.Lists["MyList"];
$list.BreakRoleInheritance($true,$true);    
$group = $web.Groups["Members of the site"];                
$roleToAdd = "Reader";
$role = $web.RoleDefinitions | where {$_.Type -eq $roleToAdd};                      
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($group);         
$assignment.RoleDefinitionBindings.Add($role);          
$list.RoleAssignments.Add($assignment); // How to remove instead ?
$list.Update();     
$web.Dispose();

Note: I do know that at least one authorization should be selected.

A solution could be to use this method. Unfortunately it's not working.

$roleToLet = "Reader"
$role = $web.RoleDefinitions | where {$_.Type -eq roleToLet };                      
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($group);         
$list.RoleAssignments[1].ImportRoleDefinitionBindings($assignment.RoleDefinitionBindings);
$list.Update();

1 Answer 1

2

The list assignments are obtained using the wrong method. Moreover, the assignments were not updated after modifications. Finally I used $web.SiteGroups instead of $web.Groups. It should be as following:

$group = $web.SiteGroups["Members of the site"];
$rolesToRemove = "Contributor";
$role = $web.RoleDefinitions | where {$_.Type -eq $rolesToRemove};                      
$assignment = list.RoleAssignments.GetAssignmentByPrincipal($group);
$assignment.RoleDefinitionBindings.Remove($role);
$assignment.update();                                                       
$list.Update();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.