Add-PnPListItem on Lookup field Error
Hi,
I'm having an issue with adding or updating a ListItem with a Lookup Field. The item is added/updated but there is an exception.
Add-PnPListItem -List "Test" -Values @{"Title"= "aaa"; "ColumnName"= 1;}
Both Add-PnPListItem and Set-PnpListItem throw the same exception.
The error is:
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
Which version of the PnP-PowerShell Cmdlets are you using?
- [x] PnP PowerShell for SharePoint Online
How did you install the PnP-PowerShell Cmdlets?
- [x] Installed through the PowerShell Gallery with Install-Module
Also happens to me regularly. Especially when a adding multiple items in a row, after a long running PS script. For example (in pseudo code):
- Connect-PnPOnline
- traverse all site collections using Get-PnPTenantSite
- traversing all O365 groups using Get-UnifiedGroup
- Connect-PnPOnline
- Add first list item using Add-PnPListItem *** error is thrown immediately after this first Add cmdlet. ****
- Add second list item using Add-PnPListItem
- Add third list item using Add-PnPListItem
It doesn't seem to happen if I remove the long running script parts, though. Update: when adding a Disconnect-PnPOnline before connecting again and adding list items, the problem doesn't seem to occur anymore.
I had a similar issue when attempting to update User fields (which as Lookup under the covers).
The fix for me was to pass the UserId as a string, even though it is an integer.
We closed this issue as it had not activity within last 180 days. This is a generic process we have decided to perform for issues, which have not been explicitly marked still to be "work in progress" based on tags. We are performing this cleaning to make sure that old issues that have already been solved (but not closed) or are no longer relevant are cleaned out and make the issues more manageable. If this issue still valid, we would ask you to open a new issue and follow the guidance in the issue template related on the recommended location. We do apologize any inconveniences this might cause. Please do remember that issues in the issue lists are also messages for others in the community, so you can also check if you can assist on any of them. “Sharing is caring!”
I had a similar issue when attempting to update User fields (which as Lookup under the covers).
The fix for me was to pass the UserId as a string, even though it is an integer.
Thanks for reporting this. I transformed the value into a string and got my script finally working