Question

I am using PnP Powershell and I am looking for a way to get the folder ID so i can set permissions to particular folder. Maybe i don't need to this the way I am trying so any advice would be appreciated.

$RootFolder = "/path/to/folder"
$Library = "Library Name"

$folders = Get-PnPFolderItem -FolderSiteRelativeUrl $RootFolder
# Break Inheritance on each folder and add the user rights
foreach ($folder in $folders){
    $user = Get-PnPUser | Where Title -Like "*$($folder.Name)*"
    $user.Email


    $folder.ListItemAllFields.BreakRoleInheritance($false,$false)
    $folder.Update()
    $folder.Context.ExecuteQuery()
    Write-Host "Inheritance broken for" $($folder.Name) -ForegroundColor Cyan
    $list = Get-PnPListItem -List $Library -Id 14 # this i need to get dynamically 

    Set-PnPListItemPermission -List 'Team Reports' -Identity $list -User $($user.Email) -AddRole 'Read' -ClearExisting

}

The $list variable is what i am having a to figure out the folder ID. Right now i can put it in manually and it does what I expect but I need it to filter based on the folder so I can set the permissions based on that folder name.

Thanks

Was it helpful?

Solution

Updated solution:

We have to load the ListItemsAllFields first.

Use

$folder.Context.Load($folder.ListItemAllFields)
$folder.Context.ExecuteQuery()
Set-PnPListItemPermission -List 'Team Reports' -Identity $folder.ListItemAllFields.Id -User $($user.Email) -AddRole 'Read' -ClearExisting 

But be carefull. $folders will contain not only the visible folders of the root folder of list. It will also contain hidden folders like attachments and items.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top