Advertisement
private775

SharePoint library replace permission levels

Apr 1st, 2015
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function replacePremissions($fld){
  2.     if($fld.Item -ne $null -and $fld.Item.HasUniqueRoleAssignments ){
  3.         Write-Host "D:$($fld.ServerRelativeUrl)"
  4.         updatePremissionsNotDelete($fld.Item)
  5.     }
  6.     if($fld.SubFolders.Count -gt 0){
  7.         foreach($d in $fld.SubFolders){
  8.             replacePremissions $d
  9.         }
  10.     }
  11. }
  12.  
  13. function updatePremissionsNotDelete($itm){
  14.     $perms2replace = @("Full Control", "Contribute")
  15.     $permReplaceWith = "Contribute not Delete"
  16.     $rdNew = $w.RoleDefinitions|?{$_.Name -eq $permReplaceWith }
  17.     foreach($rdName in $perms2replace){
  18.         $rd = $w.RoleDefinitions|?{$_.Name -eq $rdName }
  19.         foreach($ra in $itm.RoleAssignments){
  20.             if($ra.RoleDefinitionBindings.Contains($rd)){
  21.                 $ra.RoleDefinitionBindings.Remove($rd)
  22.                 $ra.RoleDefinitionBindings.Add($rdNew)
  23.             }
  24.             $ra.Update()
  25.         }
  26.     }
  27. }
  28.  
  29. $w = Get-SPWeb http://xxxxxx.com/sites/Policies
  30. $lists = @( "KEMH", "MWI")
  31.  
  32.  
  33. foreach($list in $lists){
  34.     $l = $w.Lists.TryGetList($list)
  35.  
  36.     if($l -eq $null){
  37.         throw "List not found: $($list)"
  38.     }
  39.  
  40.     updatePremissionsNotDelete $l
  41.     $rf = $l.RootFolder
  42.     replacePremissions $rf
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement