Sunday, May 1, 2016

SharePoint Online: Grant Permission to List or Library using PowerShell

Granting access to lists, libraries and individual items in SharePoint online:
There are times you may want to grant permissions at list or library level to users and groups in SharePoint online. Say for e.g. You may want to provide read access at site level and edit rights on individual list level. So, to grant permissions to lists and libraries, as a first step we have to stop inheriting permissions from its parent and then apply unique security permissions to any level underneath site collection such as: Sub-site, List, Library or list items.

How to grant access to a list or library in SharePoint online:

  • Go to the target list or library settings (From the library page, Click on Library tab on the ribbon >> Select list settings. If modern UI is enabled, head on to Site connects and click settings from the list context menu)
  • On the List Settings page, in the permissions and management group, click on "Permissions for this list" link.
  • In the permissions page, if the list is inheriting permissions from parent, we have to break the permission inheritance. Click on "Stop inheriting Permissions" button.
  • Now, from the ribbon, click Grant Permissions button from Grant group.
  •  In the Share dialog box, in the designated text box, enter names or email addresses. 
    sharepoint online grant list permissions using powershell
  • Click the Show Options button and then specify email invitation option, appropriate permission level such as edit.
  •  Click Share
SharePoint Online - PowerShell to Grant permissions to List or Library to a User or Group:
SharePoint online list permissions can be manipulated with PowerShell. Here is the a typical SharePoint online set permissions PowerShell script
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Configuration Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/Projects/"
$ListName="Project Documents"
$GroupName="Project Members"
$PermissionLevel="Read"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
  
    #Get the web and List
    $Web=$Ctx.Web
    $List=$web.Lists.GetByTitle($ListName)
    
    #Break Permission inheritence - keep existing list permissions & Item level permissions
    $List.BreakRoleInheritance($True,$True)
    $Ctx.ExecuteQuery()
    Write-host -f Yellow "Permission inheritance broken..."
    
    #Get the group or user
    $Group =$Web.SiteGroups.GetByName($GroupName) #For User: $Web.EnsureUser('salaudeen@crescent.com')
    $Ctx.load($Group)
    $Ctx.ExecuteQuery()

    #Grant permission to Group      
    #Get the role required
    $Role = $web.RoleDefinitions.GetByName($PermissionLevel)
    $RoleDB = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
    $RoleDB.Add($Role)
        
    #Assign list permissions to the group
    $Permissions = $List.RoleAssignments.Add($Group,$RoleDB)
    $List.Update()
    $Ctx.ExecuteQuery()
    Write-Host "Added $PermissionLevel permission to $GroupName group in $ListName list. " -foregroundcolor Green
}
Catch {
    write-host -f Red "Error Granting Permissions!" $_.Exception.Message
}  



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...