Set pnplistitempermission

SharePoint Online: Grant Permission to List Item using PowerShell

July 6, 2021 3 Comments list item level permission in sharepoint online, sharepoint online list item permissions

Permissions are hierarchical in SharePoint from the Top Site collection till the List Item level. When items are created in a list or library, they inherit the permissions of that list or library. However, This inheritance can be broken and permissions can be applied directly to the items. To set unique permissions on list items, you need to configure permissions on the item level. Here is how:

How to Grant Access to Individual List Items in SharePoint Online?

Got a business requirement to grant permissions at the List item level. To set explicit permissions on SharePoint Online list items, we need to break the permission inheritance first [stop inheriting permissions] and then add a user or group to the List Item.

  • Go to your SharePoint Online list or library >> Select the Item to which you want to provide unique permissions.
  • Click on the Shared With button from the ribbon. On the Shared With page, click Advanced.
  • On the Permissions tab, in the Inheritance group, click the Stop Inheriting Permissions button. Confirm the prompt.
  • Now, from the ribbon, click on the Grant Permissions. button. In the Share dialog box, enter names, email addresses. Click the Show Options button. In the Select A Permission Level list box, select appropriate permission level such as Edit.
  • Click Share.

Having too many Item level permissions often leads to performance issues! so, be careful.

SharePoint Online: Set List Item Permissions using PowerShell:

How to give item level permission for SharePoint Online? Here is my PowerShell to grant permissions in SharePoint Online

#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" #To call non-generic method Load[list, x => x.HasUniqueRoleAssignments] Function Invoke-LoadMethod[] { param[ [Microsoft.SharePoint.Client.ClientObject]$Object = $[throw "Please provide a Client Object"], [string]$PropertyName ] $ctx = $Object.Context $load = [Microsoft.SharePoint.Client.ClientContext].GetMethod["Load"] $type = $Object.GetType[] $clientLoad = $load.MakeGenericMethod[$type] $Parameter = [System.Linq.Expressions.Expression]::Parameter[[$type], $type.Name] $Expression = [System.Linq.Expressions.Expression]::Lambda[[System.Linq.Expressions.Expression]::Convert[[System.Linq.Expressions.Expression]::PropertyOrField[$Parameter,$PropertyName],[System.Object] ], $[$Parameter]] $ExpressionArray = [System.Array]::CreateInstance[$Expression.GetType[], 1] $ExpressionArray.SetValue[$Expression, 0] $clientLoad.Invoke[$ctx,@[$Object,$ExpressionArray]] } Function Set-ListItemPermission { param [ [Parameter[Mandatory=$true]] [string]$SiteURL, [Parameter[Mandatory=$true]] [string]$ListName, [Parameter[Mandatory=$true]] [string]$ItemID, [Parameter[Mandatory=$true]] [string]$PermissionLevel, [Parameter[Mandatory=$true]] [string]$UserID ] Try { #Setup Credentials to connect $Cred= Get-Credential $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials[$Cred.Username, $Cred.Password] #Setup the context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext[$SiteURL] $Ctx.Credentials = $Credentials #Get the List and Item $List = $Ctx.Web.Lists.GetByTitle[$ListName] $ListItem=$List.GetItemByID[$ItemID] $Ctx.Load[$List] $Ctx.Load[$ListItem] $Ctx.ExecuteQuery[] #Check if Item has unique permission already Invoke-LoadMethod -Object $list -PropertyName "HasUniqueRoleAssignments" $Ctx.ExecuteQuery[] #Break Item's permission Inheritance, if its inheriting permissions from the parent if [-not $ListItem.HasUniqueRoleAssignments] { $ListItem.BreakRoleInheritance[$false, $false] #keep the existing permissions: No - Clear listitems permissions: No $ctx.ExecuteQuery[] } #Get the User $User = $Ctx.Web.EnsureUser[$UserID] $Ctx.load[$User] $Ctx.ExecuteQuery[] #Get the role $Role = $Ctx.web.RoleDefinitions.GetByName[$PermissionLevel] $RoleDB = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection[$Ctx] $RoleDB.Add[$Role] #Assign permissions $UserPermissions = $ListItem.RoleAssignments.Add[$User,$RoleDB] $ListItem.Update[] $Ctx.ExecuteQuery[] Write-host -f Green "Permission granted to List Item successfully!" } Catch { Write-host -f Red "Error granting permission to List Item!" $_.Exception.Message } } #Set parameter values $SiteURL="//crescent.sharepoint.com" $ListName="Projects" $ItemID="1" $UserID="[emailprotected]" $PermissionLevel="Edit" #Call the function Set-ListItemPermission -SiteURL $SiteURL -ListName $ListName -ItemID $ItemID -UserID $UserID -PermissionLevel $PermissionLevel

This script grants permission on the Item level for a given user. If you want to provide permission to SharePoint Group, Instead of line

$User = $Web.EnsureUser[$UserAccount] #use: $Group =$Web.SiteGroups.GetByName[$GroupName] #and then $GroupPermissions = $Item.RoleAssignments.Add[$Group,$RoleDB]

PnP PowerShell to Set Item Level Permission

To change the permission for list items in SharePoint Online using PowerShell, use: Set-PnPListItemPermission cmdlet.

#Config Variables $SiteURL = "//crescenttech.sharepoint.com/sites/Marketing" $ListName ="Projects" $ListItemID ="1" $GroupName="Marketing Members" $UserID="[emailprotected]" #Connect to PnP Online Connect-PnPOnline -Url $SiteURL -Credentials [Get-Credential] #Get the Group $Group = Get-PnPGroup | where-Object {$_.Title -eq $GroupName} #Grant permission to Group - Remove all existing permissions Set-PnPListItemPermission -Identity $ListItemID -List $ListName -AddRole "Read" -Group $Group -ClearExisting #Grant permission to User Set-PnPListItemPermission -Identity $ListItemID -List $ListName -AddRole "Edit" -User $UserID

You can get all available permissions to add or remove using Get-PnPRoleDefinition cmdlet. We can grant permission to all list items as in: SharePoint Online: Grant Permission to All Items in a List using PowerShell

Related Posts

Video liên quan

Chủ Đề