Forum Discussion

Deleted's avatar
Deleted
Mar 10, 2017

Add-PnPListItem triggers an error when used with values

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.

  • Also it might be better to :- $new_item = Add-PnPListItem -List:$list Set-PnPListItem -Identity: $new_item.Id -List:$list -Values @(.................)
    • Deleted's avatar
      Deleted
      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?
      • Nigel_Price9911's avatar
        Nigel_Price9911
        Iron Contributor

        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

  • Amir Khan's avatar
    Amir Khan
    Copper Contributor

    Trying returning your statement into a variable. so then:

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

    This seems to solve the problem for me
    • Deleted's avatar
      Deleted

      I will give that a try Amir, thank you.

  • 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

     

    • Deleted's avatar
      Deleted

      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?

         

         

         

         

         

         

Resources