pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
987 stars 662 forks source link

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

Open Chris-Leeworthy opened 7 years ago

Chris-Leeworthy commented 7 years ago

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?

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?

crshnbrn66 commented 7 years ago

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

thllbrg commented 7 years ago

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.

FranckyC commented 7 years ago

Same issue for me!

Pieter-Veenstra commented 7 years ago

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

Pieter-Veenstra commented 7 years ago

Same issue here:

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

Chris-Leeworthy commented 7 years ago

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"

mrik23 commented 7 years ago

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.

Chris-Leeworthy commented 7 years ago

@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 commented 7 years ago

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

I guess something changed between the January and February versions?

NoahStahl commented 7 years ago

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.)

spurr82 commented 7 years ago

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

kmosti commented 7 years ago

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
kachihro commented 7 years ago

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

mrik23 commented 7 years ago

@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.

FranckyC commented 7 years ago

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
thisispr commented 7 years ago

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

diahughes commented 7 years ago

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.

jpalo commented 7 years ago

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"}
MartinHatch commented 7 years ago

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

Adding a valid -ContentType parameter also worked for me.

NPrice99 commented 7 years ago

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.

SanderSiep commented 7 years ago

Found the issue which is causing this problem. When using the PnP commandlet Add-PnPListItem -List "ListName" -Values @{"Title" = "Some title"} it is failing while it tries to set the content type to '0x01' and the default custom list contains 2 content types which are starting with '0x01' namely Item and Folder. When specifying the complete Content Type this is working correctly. Strange thing is that when using Add-PnPListItem -List "ListName" -Verbose it is working while it sends the correct Content Type, in my case '0x010096B52006876EDE439EE7782F47A46F11'

halfice commented 7 years ago

Below worked for me. Add-PnPListItem -List "Type of License" -ContentType "Item" -Values @{"Title" = "Cleaning Contract"}

jpalo commented 7 years ago

Can confirm that the ContentType argument helps resolve the issue.

jarleosmund commented 7 years ago

Using Get-PnPContentType, i found that the ContentType was translated to my language (norwegian), so I had to do this: Add-PnPListItem -List "somelist" -ContentType "Element" -Values @{"Ti....... Element is translated from Item. Is this a bug, I wonder.

paoIpao commented 7 years ago

Me too. I need translate ContentType to "Elemento" to work it with italian Language!

gonadn commented 7 years ago

adding -ContentType worked for me, i had to put -ContentType "Element", and not "Item".

garrytrinder commented 7 years ago

Just come across this issue, still present on SharePointPnPPowerShellOnline 2.18.1709.1 Adding -ContentType fixed issue

ustimon commented 6 years ago

Adding content type is a workaround. You can get the default list content type like this: $defaultContentType = (Get-PnPContentType -List "myListName")[0] # First ct is always a default one $listItem = Add-PnPListItem -List "myListName" -ContentType $defaultContentType -Values $listItemMetadata