Quantcast
Channel: SharePoint – SharePoint Rider
Viewing all articles
Browse latest Browse all 55

Set Permission to List Items using PowerShell

$
0
0


Add-PSSnapin Microsoft.SharePoint.Powershell

$url = "http://weburl"
$listName = "List Name";
$permissionLevel = "Permission Level Name";
$groupName = "Enter Group Name";

#Grant permission on all uniquely secured list items to the specified group

$web = Get-SPWeb $url;
$list = $web.Lists[$listName];
$permission = $web.RoleDefinitions[$permissionLevel];
$principal = $web.SiteGroups[$groupName];

#Process each list item

foreach ($item in $list.Items) {
Write-Output ("Item #" + $item.ID.ToString());

#Check to see whether the item is uniquely secured

if ($item.HasUniqueRoleAssignments -eq $FALSE) {
Write-Output " No change, permissions are inherited.";
}

else {

#Find an existing role assignment for this principal

$assignments = $item.RoleAssignments;
$assignment = $assignments | where {$_.Member.Name -eq $principal.Name};
if ($assignment -eq $NULL) {

#Add a new role assignment for the principal

$assignment = new-object Microsoft.SharePoint.SPRoleAssignment($principal);
$assignment.RoleDefinitionBindings.Add($permission);
$assignments.Add($assignment);
Write-Output (" Granted " + $permissionLevel + " to " + $groupName);
}

elseif ($assignment.RoleDefinitionBindings.Contains($permission) -ne $TRUE) {

#Update the principal's role assignment to add the desired permission level

$assignment.RoleDefinitionBindings.Add($permission);
$assignment.Update();
Write-Output (" Updated " + $groupName + " permissions to " + $permissionLevel);
}
else {
Write-Output " No change.";
}
}

}
$web.Dispose();


Filed under: Powershell, SharePoint

Viewing all articles
Browse latest Browse all 55

Trending Articles