Mar 10 2017 07:48 AM
Mar 10 2017 07:48 AM
Hi folks,
I'm new to SharePoint and developing for it so this could be a user error on my part but I'm really stuck and hope someone can help.
I wrote a simple powershell script to connect to a site and add a list item.
The script connects to the SharePoint Online site OK, but as soon as it tries to run the Add-PnPListItem command with values it crashes with an error
Add-PnPListItem : Operation is not valid due to the current state of the object.
If I change the script to run Add-PnPListItem without any values it will quite happily create a new, blank item in my list, but as soon as I try to specify any values it crashes.
Here's the script...
# Site Collection URL or Sub Site URL $siteurl = "https://xxxx.sharepoint.com/sites/migrat" # User Credentials $credential = Get-Credential # Connects and Creates Context Connect-PnPOnline -Url $siteurl -Credentials $credential # Add List Item Function # Input Parameters $listName = "PnP Test" $itemValues = @{"Title"="Woffle";} # Item Values # Creates item with values Add-PnPListItem -List $listName -Values $itemValues Disconnect-PnPOnline
The list is called PnP Test and has just the default columns.
I've tried all sorts of rewrites but always wind up at the same error. Can anyone tell me what I'm doing wrong?
Thanks in Advance
Chris.
Mar 10 2017 07:59 AM
The - List parametr expects a list object.
So it is better to include this
$list = Get-PnPList -Identity $listname Add-PnPListItem -List $list -Value $itemValues
Mar 11 2017 07:36 AM
Pieter,
I tried your advice but it still crashes with the same error.
It doesn't look like that's the issue.
Mar 13 2017 02:34 AM
Hi @Deleted,
I've reproduced the error on a task list. Then I created a new custom list and I got the same issue.
Then I tried the same in a differnt tenant and it all worked fine.
The only other differnce is that I'm running PnP PowerShell froma differnt machine today. Friday I had a slightly later version of PnP. Could you try this with the March release?
Mar 13 2017 02:45 AM
Here the same error with the list item, appears that is happens since the februari release of pnp.
Mar 13 2017 07:59 AM
It looks like I had the January release last week.
@Vesa Juvonen, it looks like a bug was introduced in February
Mar 15 2017 10:40 AM
Hi Pieter,
I get the same results with the February and March versions.
When I installed the January version the script worked as expected.
Maybe this is a bug introduced in the February update?
Mar 16 2017 02:22 AM
@Deleted,
Yes it looks like this was introduced in February. One workaround would be to run the Add-PnPListItem (without parameters) and then run Set-PnPListItem to set the properties.
Mar 16 2017 04:08 AM
Thanks Pieter,
I'm going to stick with the January version of the cmdlets for now as they seem to do the job.
Mar 23 2017 04:23 PM
Read in another thread that if we add Contenttype attribute, it works fine.
Tried and it worked.
Add-PnPListItem -List $ImportStatusListName -Values $ItemPropertiesHT -ContentType "Item"
Sourece - https://github.com/SharePoint/PnP-PowerShell/issues/778
Oct 03 2017 03:37 AM
Trying returning your statement into a variable. so then:
$item = Add-PnPListItem -List $listName -Values $itemValues
This seems to solve the problem for me
Oct 04 2017 03:27 AM
Oct 06 2017 01:40 AM
Oct 06 2017 01:41 AM
I will give that a try Amir, thank you.
Oct 10 2017 12:41 AM - edited Oct 10 2017 12:43 AM
Hi Chris
The Add-PnPListItem command adds a new, blank (empty) list item to the list.
and saves it in a variable $new_item.
The Set-PnPListItem command then uses the id of the empty list item ($new_item.id) to retrieve it and updates it with the values set out in the Set-PnPListItem command.
Regards
Nigel