Error PnP Power Shell:- The collection has not been initialized. It has not been requested or the request has not been executed

john john 1,021 Reputation points
2022-08-29T21:28:20.613+00:00

I have the following PnP Power shell, to get all the root folders of a SharePoint document library:-

#Parameters  
$SiteURL="https://***.sharepoint.com/"  
$FolderSiteRelativeURL = "Shared Documents"  
   
#Connect to the Site collection  
Connect-PnPOnline -URL $SiteURL -UseWebLogin  
   
#Get the Folder from site relative URL  
$Folder = Get-PnPFolder -Url $FolderSiteRelativeURL  
   
#Get all Subfolders of a folder - recursively  
$SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder -Recursive  
  
ForEach($SubFolder in $SubFolders)  
    {  
If($SubFolder.ListItemAllFields.HasUniqueRoleAssignments)  
{  
    Write-host "Folder is already with broken permissions!" -f Yellow  
}  
Else  
{  
Write-host "Else is running" -f Yellow  
    #Break Folder permissions - keep all existing permissions & keep Item level permissions  
    $SubFolder.ListItemAllFields.BreakRoleInheritance($True,$True)  
    Invoke-PnPQuery  
    $RoleAssignments = $SubFolder.ListItemAllFields.RoleAssignments  
    ForEach($RoleAssignment in $RoleAssignments)  
        {  
            #RoleAssignment.Remove();  
        }  
   
    Write-host "Folder's Permission Inheritance is broken!!" -f Green     
}  
    }  

but i got this error:-

The collection has not been initialized. It has not been requested or the request has not been executed. It may need  
to be explicitly requested.  
At line:14 char:9  
+ ForEach($RoleAssignment in $RoleAssignments)  
+         ~~~~~~~~~~~~~~~  
    + CategoryInfo          : OperationStopped: (:) [], CollectionNotInitializedException  
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException  
  
Folder's Permission Inheritance is broken!!  
Else is running  
The collection has not been initialized. It has not been requested or the request has not been executed. It may need  
to be explicitly requested.  
At line:14 char:9  
+ ForEach($RoleAssignment in $RoleAssignments)  
+         ~~~~~~~~~~~~~~~  
    + CategoryInfo          : OperationStopped: (:) [], CollectionNotInitializedException  
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException  
  
Folder's Permission Inheritance is broken!!  
Else is running  
The collection has not been initialized. It has not been requested or the request has not been executed. It may need  
to be explicitly requested.  
At line:14 char:9  
+ ForEach($RoleAssignment in $RoleAssignments)  
+         ~~~~~~~~~~~~~~~  
    + CategoryInfo          : OperationStopped: (:) [], CollectionNotInitializedException  
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException  
  
Folder's Permission Inheritance is broken!!  
Else is running  
Invoke-PnPQuery : Cannot invoke method or retrieve property from null object. Object returned by the following call  
stack is null. "ListItemAllFields  
"  
At line:12 char:1  
+ Invoke-PnPQuery  
+ ~~~~~~~~~~~~~~~  
    + CategoryInfo          : NotSpecified: (:) [Invoke-PnPQuery], ServerException  
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.ServerException,PnP.PowerShell.Commands.Base.InvokeQuery  
  
The collection has not been initialized. It has not been requested or the request has not been executed. It may need  
to be explicitly requested.  
At line:14 char:9  
+ ForEach($RoleAssignment in $RoleAssignments)  
+         ~~~~~~~~~~~~~~~  
    + CategoryInfo          : OperationStopped: (:) [], CollectionNotInitializedException  
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException  
  
Folder's Permission Inheritance is broken!!  
Else is running  
Invoke-PnPQuery : Cannot invoke method or retrieve property from null object. Object returned by the following call  
stack is null. "ListItemAllFields  
"  
At line:12 char:1  
+ Invoke-PnPQuery  
+ ~~~~~~~~~~~~~~~  
    + CategoryInfo          : NotSpecified: (:) [Invoke-PnPQuery], ServerException  
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.ServerException,PnP.PowerShell.Commands.Base.InvokeQuery  
  
The collection has not been initialized. It has not been requested or the request has not been executed. It may need  
to be explicitly requested.  
At line:14 char:9  
+ ForEach($RoleAssignment in $RoleAssignments)  
+         ~~~~~~~~~~~~~~~  
    + CategoryInfo          : OperationStopped: (:) [], CollectionNotInitializedException  
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException  

any advice? Thanks

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Emily Du-MSFT 51,846 Reputation points Microsoft External Staff
    2022-08-30T09:44:39.42+00:00

    @john john

    Based on provided codes, I understand that you want to remove unique permissions for a folder.

    Please try below PowerShell.

    #Parameters  
    $SiteURL="https://***.sharepoint.com/"  
    $FolderSiteRelativeURL = "Shared Documents"  
           
    #Connect to the Site collection  
    Connect-PnPOnline -URL $SiteURL -UseWebLogin  
           
    #Get the Folder from site relative URL  
    $Folder = Get-PnPFolder -Url $FolderSiteRelativeURL  
           
    #Get all Subfolders of a folder - recursively  
    $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder -Recursive  
          
    ForEach($SubFolder in $SubFolders)  
         {  
     If($SubFolder.ListItemAllFields.HasUniqueRoleAssignments)  
     {  
         Write-host "Folder is already with broken permissions!" -f Yellow  
     }  
     Else  
     {  
     Write-host "Else is running" -f Yellow  
         #Break Folder permissions - keep all existing permissions & keep Item level permissions  
         $SubFolder.ListItemAllFields.BreakRoleInheritance($True,$True)  
         Invoke-PnPQuery  
         $RoleAssignments = $SubFolder.ListItemAllFields.HasUniqueRoleAssignments  
         ForEach($RoleAssignment in $RoleAssignments)  
             {  
                 #RoleAssignment.Remove();  
             }  
           
         Write-host "Folder's Permission Inheritance is broken!!" -f Green     
     }  
         }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.