Add-PnPListItem triggers an error when used with values

Deleted
Not applicable

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.

14 Replies

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

 

Pieter,

 

I tried your advice but it still crashes with the same error.

 

It doesn't look like that's the issue.

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?

 

 

 

 

 

 

Here the same error with the list item, appears that is happens since the februari release of pnp. 

It looks like I had the January release last week.

 

@Vesa Juvonen, it looks like a bug was introduced in February

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?

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

Thanks Pieter,

 

I'm going to stick with the January version of the cmdlets for now as they seem to do the job.

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

 

 

Trying returning your statement into a variable. so then:

$item = Add-PnPListItem -List $listName -Values $itemValues

This seems to solve the problem for me
Also it might be better to :- $new_item = Add-PnPListItem -List:$list Set-PnPListItem -Identity: $new_item.Id -List:$list -Values @(.................)
I don't understand what that code is doing Nigel.

I can see the operation is returning into a variable $new_item but how can you reference $new_item.id in the operation when it won't be set until after the operation finishes?

I will give that a try Amir, thank you.

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