简体   繁体   中英

Sharepoint permissions to folders using powershell pnp

$credentail = Import-Clixml D:\cred.xml
$siteurl = "https://metho.sharepoint.com/sites/EducationandHumanities41"
$access = "Read"
$folders = Import-csv "C:\Users\metho\Desktop\sharepoint_Permissions.csv"

Connect-PnPOnline -Url $siteurl -Credentials $credentail 




Foreach ($entry in $folders) {
$ListName = $entry.'Assoc Dean Area Name'
$foldername = $entry.'Curriculum Area Name'
$foldername2 = $entry.'Course Title'
$foldername3 = $entry.c16
$Nameofthefolder = $entry.currTeamName
$emailAddress= $entry.facultyHeademail
$UserOne = $entry.DeanName


#Get-PnPFolder -Url $Listname

$ListitemID = Get-PnPListItem -List $ListName


#$listItem = Get-PnPListItem -List $Listname -Query "<View><Query><Where><Eq><FieldRef Name='Id'/><Value Type='Text'>$ListName</Value></Eq></Where></Query></View>"


Set-PnPListItemPermission -List $Listname -Identity $ListitemID -User $UserOne -AddRole $access 


}

I am struggling to Apply the permissions to all the sub-folders under a SharePoint "Document Library". I am struggling (Which i think is the Issue) with the IDs of sub-folders. If i add the ID manually then it would apply the permissions to that particular ID of folder but i am looking to apply to all the sub-folders under a Document Library.

Set-PnPListItemPermission : Cannot convert 'System.Object[]' to the type 'SharePointPnP.PowerShell.Commands.Base.PipeBinds.ListItemPipeBind' required by parameter 'Identity'. Specified method is not supported.
At line:31 char:53
+ ... t-PnPListItemPermission -List $Listname -Identity $ListitemID -User $ ...
+                                                       ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-PnPListItemPermission], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,SharePointPnP.PowerShell.Commands.Lists.SetListItemPermission

The command Get-PnPListItem would retrieve all list items from the list. For your requiremments, you need to Iterate through each folders in the document library and apply permissions.

Here are my demos:

$listname="test"
$SiteURL = "https://tenant.sharepoint.com/sites/michael"
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
$ParentFolderURL = "/test" #Site Relative Path of the document Library
$UserAccount = "michael@domain.com"
$AllFolders= Get-PnPFolderItem -ItemType Folder -FolderSiteRelativeUrl $ParentFolderURL | Where {($_.Name -ne "Forms") -and (-Not($_.Name.StartsWith("_")))}

ForEach($Folder in $AllFolders) { 
 #Grant Contribute permissions to the Folder 
Set-PnPListItemPermission -List $ListName -Identity $Folder.ListItemAllFields -User $UserAccount -AddRole 'Read' 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM