PnP-PowerShell icon indicating copy to clipboard operation
PnP-PowerShell copied to clipboard

Add-PnPListItem - Operation is not valid due to the current state of the object

Open Chris-Leeworthy opened this issue 9 years ago • 28 comments

Reporting an Issue or Missing Feature

Issue

Expected behaviour

Working with a pre-existing list in a site collection with just the default Title field defined.

Using the cmdlet with the -Values parameter should create a list item populated with the specified values.

Actual behavior

An error occurs (see output below)

Add-PnPListItem : Operation is not valid due to the current state of the object.
At C:\Users\xxxx\Documents\Untitled1.ps1:17 char:5
+     Add-PnPListItem -List $listName -Values $itemValues
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Add-PnPListItem], ServerException
    + FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Lists.AddListItem

Steps to reproduce behaviour

Create a custom list with just the default fields (used SharePoint web interface rather than scripting). Then run the following script...

# Site Collection URL or Sub Site URL
$siteurl = "https://putyoursiteurlhere.sharepoint.com/sites/sitename"

# User Credentials
$credential = Get-Credential

# Connects and Creates Context
Connect-PnPOnline -Url $siteurl -Credentials $credential

# Add List Item Function
function AddListItemWithValues(){
    # Input Parameters
    $listName = "Test List"
    $itemValues = @{"Title"="Woffle";}

    # Creates item with values
    Add-PnPListItem -List $listName -Values $itemValues
}
# Call the function
AddListItemWithValues 


Which version of the PnP-PowerShell Cmdlets are you using?

  • [ ] PnP PowerShell for SharePoint 2013
  • [ ] PnP PowerShell for SharePoint 2016
  • [X] PnP PowerShell for SharePoint Online

What is the version of the Cmdlet module you are running?

(you can retrieve this by executing Get-Module -Name *pnppowershell* -ListAvailable) Version 2.12.1702.0

How did you install the PnP-PowerShell Cmdlets?

  • [X] MSI Installed downloaded from GitHub
  • [ ] Installed through the PowerShell Gallery with Install-Module
  • [ ] Other means

Chris-Leeworthy avatar Mar 08 '17 10:03 Chris-Leeworthy

I'm having this exact same issue .... Please advise

crshnbrn66 avatar Mar 09 '17 15:03 crshnbrn66

Something seems to have changed in the past few days in SharePoint online, it first occurred yesterday for us. The problem seems to be related to SystemUpdate, which no longer work when adding an item in SharePoint Online, maybe an update to https://github.com/SharePoint/PnP-PowerShell/blob/fb01372c1d1b2146bdbd290b6acefcbff0882713/Commands/Lists/AddListItem.cs#L176 can resolve it.

thllbrg avatar Mar 09 '17 15:03 thllbrg

Same issue for me!

FranckyC avatar Mar 10 '17 23:03 FranckyC

I think you need to use a list object rather than a listname. Use get-pnplist to get the list.

Pieter-Veenstra avatar Mar 11 '17 07:03 Pieter-Veenstra

Same issue here:

https://techcommunity.microsoft.com/t5/SharePoint-Developer/Add-PnPListItem-triggers-an-error-when-used-with-values/td-p/52140

Pieter-Veenstra avatar Mar 11 '17 07:03 Pieter-Veenstra

It still crashes with the same error regardless of whether I give the list name or I pass in a list object obtained by Get-PnPList.

Either way it crashes with "Operation is not valid due to the current state of the object"

Chris-Leeworthy avatar Mar 11 '17 15:03 Chris-Leeworthy

I have the same issue with both Feb and March release of PnP PowerShell for SharePoint Online.

edit: Re-installed January release and it works again.

mrik23 avatar Mar 13 '17 09:03 mrik23

@mrik23 Interesting to know, thank you.

Do you know if there is some way for me to get hold of the older release?

Ah! Never mind, I found it, (note to self, look first then ask!)

Chris-Leeworthy avatar Mar 13 '17 10:03 Chris-Leeworthy

The January release of the PnP cmdlets works for me too.

I guess something changed between the January and February versions?

Chris-Leeworthy avatar Mar 13 '17 10:03 Chris-Leeworthy

I'm seeing the same issue on the March release. I've noticed that the command works if the ContentType parameter is specified:

Works:

Add-PnPListItem -List $list -Values $listItemInfo -ContentType "Item"

Doesn't work:

Add-PnPListItem -List $list -Values $listItemInfo

(Edit: this seems to work with a full admin but NOT as user with Contribute permission on list. Haven't probed, just abandoned this method until resolved in favor of AddItem method on List object.)

NoahStahl avatar Mar 15 '17 21:03 NoahStahl

Had same issue whilst following this blog, Channel9 demo https://channel9.msdn.com/Blogs/MVP-Azure/Work-with-SharePoint-Online-lists-with-PNP-PowerShell

spurr82 avatar Mar 20 '17 15:03 spurr82

Had the same issue, here is my workaround:

#this one fails:
$onenoteitem = @{
    Title = "Notebook"
    BackgroundImageLocation = "$onenote, $onenote"
    LinkLocation = "$($targetSiteUri.LocalPath)/_layouts/15/WopiFrame.aspx?sourcedoc=$($targetSiteUri.AbsoluteUri)/SiteAssets/Team Site Notebook&action=editnew, $($targetSiteUri.LocalPath)/_layouts/15/WopiFrame.aspx?sourcedoc=$($targetSiteUri.AbsoluteUri)/SiteAssets/Team Site Notebook&action=editnew"
    LaunchBehavior = "In page navigation"
    TileOrder = "0"
}

Add-PnPListItem -List $promotedlinkslist -Values $onenoteitem


#this does the same, but works
$list = Get-PnPList $promotedlinkslist

$itemcreateinfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$listitem = $list.AddItem($itemcreateinfo)
$listitem["Title"] = "Notebook"
$listitem["BackgroundImageLocation"] = "$onenote, $onenote"
$listitem["LinkLocation"] = "$($targetSiteUri.LocalPath)/_layouts/15/WopiFrame.aspx?sourcedoc=$($targetSiteUri.AbsoluteUri)/SiteAssets/Team Site Notebook&action=editnew, $($targetSiteUri.LocalPath)/_layouts/15/WopiFrame.aspx?sourcedoc=$($targetSiteUri.AbsoluteUri)/SiteAssets/Team Site Notebook&action=editnew"
$listitem["LaunchBehavior"] = "In page navigation"
$listitem["TileOrder"] = "0"
$listitem.Update()
Execute-PnPQuery

kmosti avatar Mar 21 '17 11:03 kmosti

Thanks @NoahStahl - that was the clincher ! I guess it's trying to create a new item, but doesn't know what content type. -ContentType Item

kachihro avatar Apr 12 '17 05:04 kachihro

@NoahStahl thanks indeed adding the content type does the trick. Not sure that's a bug or a feature :) Would be cool if the documentation get an update.

mrik23 avatar Apr 18 '17 12:04 mrik23

Same issue here with SharePoint Online and the April release (2.14.1704.0) even with the -ContentType parameter. The following code did the trick ("Add" then "Set" the item):

$Item = Add-PnPListItem -List $List
Set-PnPListItem -Identity $Item.Id -List $List -Values $Values

FranckyC avatar Apr 30 '17 16:04 FranckyC

looks similar with this: https://github.com/SharePoint/PnP-PowerShell/issues/799

thisispr avatar May 02 '17 11:05 thisispr

I have the same problem with the May Update for SharePoint OnLine Neither the solution FranckyC gave, nor the workaround kmosti showed above works.

Also after reverting back to January, I still get Add-PNPListItem : List data validation Failed, and cannot proceed with the record update.

diahughes avatar Jun 01 '17 09:06 diahughes

Interesting, for me with May 2017 release, FranckyC's tip of first adding empty item and only setting values later works in SPO.

$newItem = Add-PnPListItem -List $l 
Set-PnPListItem -List $l -Identity $newItem -Values @{"Title" = "Title goes here"; "OtherField" = "Other field value goes here"}

jpalo avatar Jun 05 '17 09:06 jpalo

Tested with the July 2017 Intermediate release, which was failing in SPO

Adding a valid -ContentType parameter also worked for me.

MartinHatch avatar Jul 12 '17 15:07 MartinHatch

Can I add my name to the long list of people affected by this issue.

Tested with the July 2017 Intermediate release.

Adding a valid -ContentType parameter also worked for me.

NPrice99 avatar Aug 03 '17 10:08 NPrice99