Thursday, October 9, 2014

Sharepoint break role inherentence and add custom permission in sharepoint library/List through Powershell

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) {
Write-Host "Loading SharePoint Powershell Snapin"
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}



function AddGroupsToList($groupName,$permissionLevel)
{
    $Group = $web.SiteGroups[$groupName]
    $GroupAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($Group)
    #Get the permission levels to apply to the new groups
    $RoleDefinition = $web.Site.RootWeb.RoleDefinitions[$permissionLevel]
    #Assign the groups the appropriate permission level
    $GroupAssignment.RoleDefinitionBindings.Add($RoleDefinition)
    #Add the group to the site with the permission level
    $list.RoleAssignments.Add($GroupAssignment)    
    $list.Update()
}


$site = Get-SPSite "http://webapplication/sites/RECMRepository/"
$web = $site.RootWeb



#Break Roleinherentence for "Public Documents" list
Write-Host  -foreground "green" "Breaking Role inherentence for the list - Public Documents"
$list = $web.Lists["Public Documents"]
$list.BreakRoleInheritance($false)
#Add Groups to the list with permission levels assigned
AddGroupsToList "Group1" "Full Control"
AddGroupsToList "Group2" "Contribute"
AddGroupsToList "Group3" "Read"


Note:Breaking role inherentence of either site or library or list with Parameter "False" will give you with clean slate no groups from top level will be copied hence I am adding the groups which I need later,

But if the parameter is "True" it will copy the groups from top level,in the case you may need to delete the unwanted groups from the list
 

No comments:

Post a Comment