2

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

2 Answers 2

5

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.

5
  • I did that before and it doesnt come up with anything for some reason. and i get "Set-PnPListItemPermission : Cannot bind argument to parameter 'Identity' because it is null". Is there a way i can list all the $folder properties?
    – jAC
    Commented Mar 15, 2018 at 19:13
  • hmm... seems like ListItemAllFields.Id is empty for some reason. I have tested it on my machine and there is no value inside.
    – Lutz
    Commented Mar 15, 2018 at 20:07
  • Yeah, not sure why it would be empty. Could i use Get-PnPListItem instead of Get-PnPFolderItem? I am trying it and looks like i could see the folders but its all guid based now instead of by name. I also only want folders in a certain directory. ListItem is showing all them regardless :/
    – jAC
    Commented Mar 15, 2018 at 20:26
  • see my updated answer.
    – Lutz
    Commented Mar 15, 2018 at 20:35
  • 2
    you can also directly use includes parameter Get-PnPFolder -Url "<path here>" -Includes "ListItemAllFields"
    – san
    Commented Jun 1, 2021 at 12:25
0

An alternative to @Lutz (perfectly correct) answer:

Get-PnPProperty -ClientObject $folder -Property ListItemAllFields | Out-Null
Write-Host "Folder: [$($folder.ListItemAllFields.Id)] $($folder.Name)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.