SharePoint Online: Fix “An error occurred while enumerating through a collection: 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” in PowerShell

Problem: Error in SharePoint Online CSOM PowerShell script:
“An error occurred while enumerating through a collection: 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”

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.

Root cause: Every collection must be explicitly loaded before accessing its members.

The Problematic Script:
This PowerShell script tries to get columns of a content type.

#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"

#parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ContentTypeID="0x01002A7A908ACAB0054880702EE263AC762B"
 
#Get Credentials to connect
$Cred= Get-Credential
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
#Get content type
$ContentType = $Ctx.web.ContentTypes.GetById($ContentTypeID)
$Ctx.Load($ContentType)
$Ctx.ExecuteQuery()
 
If($ContentType -ne $Null)
{
    #Get columns from the content type
    ForEach($Field in $ContentType.Fields)
    {           
        Write-Host -f Green $Field.Title
    } 
}
else
{
    Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
}

Here in the above script, we are trying to get columns from a content type without explicitly loading the fields collection, so it ends up with an 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:24 char:13
+     ForEach($Field in $ContentType.Fields)
+             ~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], CollectionNotInitializedException
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException

Solution:

Here is the corrected Script, where we loaded the $ContentType.Fields collection and resolved the issue.

#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"

#parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ContentTypeID="0x01002A7A908ACAB0054880702EE263AC762B"
 
#Get Credentials to connect
$Cred= Get-Credential
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
#Get content type
$ContentType = $Ctx.web.ContentTypes.GetById($ContentTypeID)
$Ctx.Load($ContentType)
$Ctx.ExecuteQuery()
 
If($ContentType -ne $Null)
{
    #Get Columns from the content type
    $Ctx.Load($ContentType.Fields)
    $Ctx.ExecuteQuery()

    #Get columns from the content type
    ForEach($Field in $ContentType.Fields)
    {           
        Write-Host -f Green $Field.Title
    } 
}
else
{
    Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
}

Similarly, in PnP PowerShell, we have to use the Get-PnPProperty cmdlet to load a collection.

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ContentTypeName ="Item"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get the List content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName
 
#Load the "Fields" collection to retrieve All Fields from the Content Type
$ContentTypeFields = Get-PnPProperty -ClientObject $ContentType -Property Fields

#Get Field Title, Internal Name and ID
$ContentTypeFields | Select Title, InternalName, ID

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply