Add-PnPListItem triggers an error when used with values

%3CLINGO-SUB%20id%3D%22lingo-sub-52140%22%20slang%3D%22en-US%22%3EAdd-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-52140%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20folks%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20new%20to%20SharePoint%20and%20developing%20for%20it%20so%20this%20could%20be%20a%20user%20error%20on%20my%20part%20but%20I'm%20really%20stuck%20and%20hope%20someone%20can%20help.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20wrote%20a%20simple%20powershell%20script%20to%20connect%20to%20a%20site%20and%20add%20a%20list%20item.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20script%20connects%20to%20the%20SharePoint%20Online%20site%20OK%2C%20but%20as%20soon%20as%20it%20tries%20to%20run%20the%20Add-PnPListItem%20command%20with%20values%20it%20crashes%20with%20an%20error%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EAdd-PnPListItem%20%3A%20Operation%20is%20not%20valid%20due%20to%20the%20current%20state%20of%20the%20object.%3C%2FPRE%3E%3CP%3EIf%20I%20change%20the%20script%20to%20run%20Add-PnPListItem%20without%20any%20values%20it%20will%20quite%20happily%20create%20a%20new%2C%20blank%20item%20in%20my%20list%2C%20but%20as%20soon%20as%20I%20try%20to%20specify%20any%20values%20it%20crashes.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere's%20the%20script...%3C%2FP%3E%3CPRE%3E%23%20Site%20Collection%20URL%20or%20Sub%20Site%20URL%0A%24siteurl%20%3D%20%22https%3A%2F%2Fxxxx.sharepoint.com%2Fsites%2Fmigrat%22%0A%0A%23%20User%20Credentials%0A%24credential%20%3D%20Get-Credential%0A%0A%23%20Connects%20and%20Creates%20Context%0AConnect-PnPOnline%20-Url%20%24siteurl%20-Credentials%20%24credential%0A%0A%23%20Add%20List%20Item%20Function%0A%23%20Input%20Parameters%0A%24listName%20%3D%20%22PnP%20Test%22%0A%24itemValues%20%3D%20%40%7B%22Title%22%3D%22Woffle%22%3B%7D%20%23%20Item%20Values%0A%0A%23%20Creates%20item%20with%20values%0AAdd-PnPListItem%20-List%20%24listName%20-Values%20%24itemValues%0A%0ADisconnect-PnPOnline%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20list%20is%20called%20PnP%20Test%20and%20has%20just%20the%20default%20columns.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20tried%20all%20sorts%20of%20rewrites%20but%20always%20wind%20up%20at%20the%20same%20error.%20%26nbsp%3BCan%20anyone%20tell%20me%20what%20I'm%20doing%20wrong%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20Advance%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EChris.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-52140%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-114986%22%20slang%3D%22en-US%22%3ERe%3A%20RE%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-114986%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Chris%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20Add-PnPListItem%20command%20adds%20a%20new%2C%20blank%20(empty)%20list%20item%20to%20the%20list.%3C%2FP%3E%3CP%3E%26nbsp%3Band%20saves%20it%20in%20a%20variable%20%24new_item.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20Set-PnPListItem%20command%20then%20uses%20the%20id%20of%20the%20empty%20list%20item%20(%24new_item.id)%20to%20retrieve%20it%20and%20updates%20it%20with%20the%20values%26nbsp%3B%20set%20out%20in%20the%20Set-PnPListItem%20command.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENigel%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-114108%22%20slang%3D%22en-US%22%3ERe%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-114108%22%20slang%3D%22en-US%22%3E%3CP%3EI%20will%20give%20that%20a%20try%20Amir%2C%20thank%20you.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-114105%22%20slang%3D%22en-US%22%3ERe%3A%20RE%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-114105%22%20slang%3D%22en-US%22%3EI%20don't%20understand%20what%20that%20code%20is%20doing%20Nigel.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20can%20see%20the%20operation%20is%20returning%20into%20a%20variable%20%24new_item%20but%20how%20can%20you%20reference%20%24new_item.id%20in%20the%20operation%20when%20it%20won't%20be%20set%20until%20after%20the%20operation%20finishes%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-113249%22%20slang%3D%22en-US%22%3ERE%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-113249%22%20slang%3D%22en-US%22%3EAlso%20it%20might%20be%20better%20to%20%3A-%20%24new_item%20%3D%20Add-PnPListItem%20-List%3A%24list%20Set-PnPListItem%20-Identity%3A%20%24new_item.Id%20-List%3A%24list%20-Values%20%40(.................)%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-112865%22%20slang%3D%22en-US%22%3ERe%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-112865%22%20slang%3D%22en-US%22%3E%3CP%3ETrying%20returning%20your%20statement%20into%20a%20variable.%20so%20then%3A%3C%2FP%3E%3CPRE%3E%24item%20%3D%20Add-PnPListItem%20-List%20%24listName%20-Values%20%24itemValues%3CBR%20%2F%3E%3CBR%20%2F%3EThis%20seems%20to%20solve%20the%20problem%20for%20me%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-56382%22%20slang%3D%22en-US%22%3ERe%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-56382%22%20slang%3D%22en-US%22%3E%3CP%3ERead%26nbsp%3Bin%20another%20thread%20that%20if%20we%20add%20Contenttype%20attribute%2C%20it%20works%20fine.%3C%2FP%3E%3CP%3ETried%20and%20it%20worked.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EAdd-PnPListItem%20-List%20%24ImportStatusListName%20-Values%20%24ItemPropertiesHT%20-ContentType%20%22Item%22%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESourece%20-%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fissues%2F778%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fissues%2F778%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-53747%22%20slang%3D%22en-US%22%3ERe%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-53747%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20Pieter%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20going%20to%20stick%20with%20the%20January%20version%20of%20the%20cmdlets%20for%20now%20as%20they%20seem%20to%20do%20the%20job.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-53712%22%20slang%3D%22en-US%22%3ERe%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-53712%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F43088%22%20target%3D%22_blank%22%3E%40Chris%20Leeworthy%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYes%20it%20looks%20like%20this%20was%20introduced%20in%20February.%20One%20workaround%20would%20be%20%26nbsp%3Bto%20run%20the%20Add-PnPListItem%20(without%20parameters)%20and%20then%20run%20Set-PnPListItem%20to%20set%20the%20properties.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-53540%22%20slang%3D%22en-US%22%3ERe%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-53540%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Pieter%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20get%20the%20same%20results%20with%20the%20February%20and%20March%20versions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20I%20installed%20the%20January%20version%20the%20script%20worked%20as%20expected.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMaybe%20this%20is%20a%20bug%20introduced%20in%20the%20February%20update%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-52650%22%20slang%3D%22en-US%22%3ERe%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-52650%22%20slang%3D%22en-US%22%3E%3CP%3EIt%20looks%20like%20I%20had%20the%20January%20release%20last%20week.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F5720%22%20target%3D%22_blank%22%3E%40Vesa%20Juvonen%3C%2FA%3E%2C%20it%20looks%20like%20a%20bug%20was%20introduced%20in%20February%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-52550%22%20slang%3D%22en-US%22%3ERe%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-52550%22%20slang%3D%22en-US%22%3E%3CP%3EHere%20the%20same%20error%20with%20the%20list%20item%2C%20appears%20that%20is%20happens%20since%20the%20februari%20release%20of%20pnp.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-52546%22%20slang%3D%22en-US%22%3ERe%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-52546%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F43088%22%20target%3D%22_blank%22%3E%40Chris%20Leeworthy%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20reproduced%20the%20error%20on%20a%20task%20list.%20Then%20I%20created%20a%20new%20custom%20list%20and%20I%20got%20the%20same%20issue.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20I%20tried%20the%20same%20in%20a%20differnt%20tenant%20and%20it%20all%20worked%20fine.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20only%20other%20differnce%20is%20that%20I'm%20running%20PnP%20PowerShell%20froma%20differnt%20machine%20today.%20Friday%20I%20had%20a%20slightly%20later%20version%20of%20PnP.%20Could%20you%20try%20this%20with%20the%20March%20release%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-52344%22%20slang%3D%22en-US%22%3ERe%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-52344%22%20slang%3D%22en-US%22%3E%3CP%3EPieter%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20your%20advice%20but%20it%20still%20crashes%20with%20the%20same%20error.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20doesn't%20look%20like%20that's%20the%20issue.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-52146%22%20slang%3D%22en-US%22%3ERe%3A%20Add-PnPListItem%20triggers%20an%20error%20when%20used%20with%20values%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-52146%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20-%20List%20parametr%20expects%20a%20list%20object.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20it%20is%20better%20to%20include%20this%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24list%20%3D%20Get-PnPList%20-Identity%20%24listname%0AAdd-PnPListItem%20-List%20%24list%20-Value%20%24itemValues%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
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
Highlighted

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

 

Highlighted

Pieter,

 

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

 

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

Highlighted

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?

 

 

 

 

 

 

Highlighted

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

Highlighted

It looks like I had the January release last week.

 

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

Highlighted

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?

Highlighted

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

Highlighted

Thanks Pieter,

 

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

Highlighted

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

 

 

Highlighted

Trying returning your statement into a variable. so then:

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

This seems to solve the problem for me
Highlighted
Also it might be better to :- $new_item = Add-PnPListItem -List:$list Set-PnPListItem -Identity: $new_item.Id -List:$list -Values @(.................)
Highlighted
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?
Highlighted

I will give that a try Amir, thank you.

Highlighted

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