CollectionNotInitializedException when reading list items #265
Replies: 2 comments 1 reply
-
This is actually not an error, althought it surfaces as an error. I know :-) What is happening is the following: behind the scenes we use CSOM (SharePoint Client Side Object Model) to make a call to the server to retrieve the list item. By default CSOM tries to be 'efficient', that means that it only retrieves certain properties from the server, and if someone wants to see more properties of an artifact like a listitem, they will have to explicitly ask for that at the time of the request. We cannot guess ahead of time what you want to see of a listitem, so we only load a subset of those properties out of the box. PowerShell is not aware of this behavior, and CSOM has been written in such a way that the moment you access property that has not been loaded it will throw the exception you posted: collection has not been initialized. However, if you use I assume that the main interest of a listitem is the field values. In that case use this: $list1 = Get-PnPList 'SharePoint List'
$items = Get-PnPListItem List -$list1.Id
$items[0].FieldValues |
Beta Was this translation helpful? Give feedback.
-
Lazy loading is not a new thing wither, it was exactly the same in the deprecated module 😉Also remember you can use -Fields parameter to load specific fields (or -Includes for other cmdlets like Get-Pnp List) to get properties like RoleAssignmentKind regards Anders Rask On 12 Feb 2021 18.19, Erwin van Hunen <[email protected]> wrote:
This is actually not an error, althought it surfaces as an error. I know :-)
What is happening is the following: behind the scenes we use CSOM (SharePoint Client Side Object Model) to make a call to the server to retrieve the list item. By default CSOM tries to be 'efficient', that means that it only retrieves certain properties from the server, and if someone wants to see more properties of an artifact like a listitem, they will have to explicitly ask for that at the time of the request.
We cannot guess ahead of time what you want to see of a listitem, so we only load a subset of those properties out of the box. PowerShell is not aware of this behavior, and CSOM has been written in such a way that the moment you access property that has not been loaded it will throw the exception you posted: collection has not been initialized.
However, if you use select * PowerShell will try to access -all- the properties the object has, regardless if they have been loaded. And the moment it accesses a not loaded property, CSOM will throw that error.
I assume that the main interest of a listitem is the field values. In that case use this:
$list1 = Get-PnPList 'SharePoint List'
$items = Get-PnPListItem List -$list1.Id
$items[0].FieldValues
—You are receiving this because you are subscribed to this thread.Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
-
Hi
Since this is now a "new" module, it is a good opportunity to get an answer on the CollectionNotInitializedException error.
There are many result in Google but nothing actually useful.
Question:
What is wrong with this code and why I cannot read list item properties ?
ipmo PnP.PowerShell
Connect-PnPOnline -Url 'https://mytenant.sharepoint.com/sites/SharedMailboxesRequests' -UseWebLogin
$list1 = Get-PnPList 'Sharepoint List'
$items = Get-PnPListItem -List $list1.Id
$items[0] | select *
Result is always this:
format-default : 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. + CategoryInfo : NotSpecified: (:) [format-default], CollectionNotInitializedException + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefaultCommand
I am running Windows PowerShell 5.1.19041.610 and module version 1.3.0
Beta Was this translation helpful? Give feedback.
All reactions