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
Let's taken another example to retrieve all list names from a SharePoint Online site:
Here, in this case, we should load the Lists collection first. So, to fix the issue, use:
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://crescenttech.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 the Line # 19 and 20, you would end up with the error The collection has not been initialized!
Let's taken 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:
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 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 loop!
No comments:
Please Login and comment to get your questions answered!