SharePoint Online: Fix “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” Error in PowerShell
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.
Root Cause:
You must load the object before you are trying to retrieve its properties!
Solution:
Here is how the problem can be resolved:
#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"
$UserName = "[email protected]"
$Password = "Password goes here"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
#Get All Fields from the List
$List = $Ctx.Web.Lists.GetByTitle("Documents")
#Load List Fields collection
$Ctx.Load($List.Fields)
$Ctx.ExecuteQuery()
ForEach($Field in $List.Fields)
{
Write-host $Field.Title
}
Please note, if you skip Line # 19 and 20, you would end up with the error The collection has not been initialized! Let’s take another example to retrieve all list names from a SharePoint Online site:
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Projects"
#Setup 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 all List names from the site
$Lists = $Ctx.web.Lists
ForEach($List in $Lists)
{
Write-host $List.Title
}
The above script fails with an error message:
At C:\Users\Salaudeen\Documents\PowerShell\Get-Lists.ps1:15 char:9
+ ForEach($List in $Lists)
+ ~~~~~
+ CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException
Here, in this case, we should load the Lists collection first. So, to fix the issue, use:
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking #Parameters $SiteURL = "https://crescent.sharepoint.com/sites/Projects" #Setup 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 all List names from the site $Lists = $Ctx.web.Lists $Ctx.Load($Lists) $Ctx.ExecuteQuery() ForEach($List in $Lists) { Write-host $List.Title }
So, make sure collection objects are loaded and executed before accessing them inside the loop!
Hello there,
I am getting this error:
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..
At line:1 char:1
+ $Folder.Files
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Microsoft.Share…nt.Client.File]:d__0) [], RuntimeException
+ FullyQualifiedErrorId : BadEnumeration
I cannot for the life of me seem to get the collection to initialize. Can you help? Code here:
Function Get-FilesFromFolder()
{
Try
{
#Load credentials of the admin account that has access to the library
$Cred = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName, $Cred.Password)
#Building Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the library
$Library = $Ctx.Web.Lists.GetByTitle($LibraryName)
$Folders = $Library.RootFolder.Folders
$Ctx.Load($Folders)
$Ctx.ExecuteQuery()
#Get the folder by name
$Folder = $Folders | Where {$_.Name -eq $FolderName}
$Ctx.Load($Folder)
$Ctx.ExecuteQuery()
#Iterate through each file
Foreach($File in $Folder.Files)
{
#Write out each file name
Write-Host “Now printing names for files in the folder.”
Write-Host “There are “$Folder.ItemCount” files in the folder.”
Write-Host -f Green $File.Name
}
}
Catch
{
Write-Host -f Red “Error getting files.” $_.Exception.Message
}
}
Hi. I am having an issue where the $ctx.Web collection is not initialised and try as I might, I cannot find a way to intialise/load it. Any ideas?
Try loading the web object. E.g.