Tuesday, February 6, 2018

Provide Access to User on All list/libraries under Sites using PowerShell

# create object for site and provide SharePoint site url
$objSite = Get-SPSite -Identity "http://SHAREPOINT-SITE-URL"

# create object for user. you need to give user id with domain
$objUser = Get-SPUser -Identity "DOMAIN\USERID" -Web $objSite.RootWeb

# create role assignment object for user
$objAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($objUser) ;        

# loop though sites and list/libraries to grant full control
foreach ($objWeb in $objSite.AllWebs) 
{            
foreach ($objList in $objWeb.Lists) 
{
if($objList.HasUniqueRoleAssignments -eq $true)
{
$objRole = $objWeb.RoleDefinitions["Full Control"];
$objAssignment .RoleDefinitionBindings.Add($objRole);           
$objList.RoleAssignments.Add($objAssignment) ;
$objList.Update();     
    }             
}
$objWeb.Dispose(); 
}

No comments:

Post a Comment