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"

Root cause: Every collection must be explicitly loaded prior 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.Field 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
}


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 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 Reviewed by Salaudeen Rajack on July 13, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.