powershell icon indicating copy to clipboard operation
powershell copied to clipboard

Error when using batch with Add-PNPListItem

Open JAK1047 opened this issue 5 years ago • 10 comments

Reporting an Issue or Missing Feature

When using the -Batch switch off Add-PnPListItem I'm getting an error. It works fine when removing the batch option and works fine using -Batch on Set-PnPListItem

Expected behavior

Create a simple hash table for adding to a SPO list:

Name Value

HR_REP_MAIL [email protected] HIRE_DT 5/10/2021 FULL_PART_TIME F REP_NAME Doe, John AC_BUSINESS_UNIT 99999999 MANAGER N BARG_UNIT N POSITION_NBR 99999999 SERVICE_DT 5/10/2021 WIN_HRBP_ID e9999999 STATE_WORK NC DEPT_DESCR Kinetic LOCATION V188 AC_SOA 0 LOCATION_DESCR Virtual Office NC Title e999999

Use this to try and add a list item via batch:

Add-PnPListItem -List 'HRData' -Values $CreationHash -Batch $Batch

Actual behavior

A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.

What is the version of the Cmdlet module you are running?

1.5.0

Which operating system/environment are you running PnP PowerShell on?

  • [ X] Windows
  • [ ] Linux
  • [ ] MacOS
  • [ ] Azure Cloud Shell
  • [ ] Azure Functions
  • [ ] Other : please specify

Error (truncated for readability):

Exception : Type : System.Text.Json.JsonException Path : $.Members.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.Value.OverloadDefinitions Message : A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Name : ThrowJsonException_SerializerCycleDetected DeclaringType : System.Text.Json.ThrowHelper, System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 StackTrace : at System.Text.Json.ThrowHelper.ThrowJsonException_SerializerCycleDetected(Int32 maxDepth) at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.JsonSerializer.WriteCore[TValue](JsonConverter jsonConverter, Utf8JsonWriter writer, TValue& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.JsonSerializer.WriteCore[TValue](Utf8JsonWriter writer, TValue& value, Type inputType, JsonSerializerOptions options) at System.Text.Json.JsonSerializer.Serialize[TValue](TValue& value, Type inputType, JsonSerializerOptions options) at System.Text.Json.JsonSerializer.Serialize(Object value, Type inputType, JsonSerializerOptions options) at PnP.Core.Model.SharePoint.ListItem.<.ctor>b__2_2(Dictionary2 keyValuePairs) at PnP.Core.Model.BaseDataModel1.AddBatchAsync(Batch batch, Dictionary2 keyValuePairs) at PnP.Core.Model.SharePoint.ListItemCollection.AddBatchAsync(Batch batch, Dictionary2 values, String folderPath, FileSystemObjectType fileSystemObjectType) at PnP.Core.Model.SharePoint.ListItemCollection.AddBatch(Batch batch, Dictionary2 values, String folderPath, FileSystemObjectType fileSystemObjectType) at PnP.PowerShell.Commands.Lists.AddListItem.ExecuteCmdlet() in c:\PnPPowerShell\src\Commands\Lists\AddListItem.cs:line 125 at PnP.PowerShell.Commands.PnPSharePointCmdlet.ProcessRecord() in c:\PnPPowerShell\src\Commands\Base\PnPSharePointCmdlet.cs:line 91

JAK1047 avatar May 12 '21 14:05 JAK1047

I have a related issue, but it behaves a little bit differently. Still, I believe, the cause of the issue is the same - something happens in the Serialization behind the scenes, .NET Core?

To reproduce my scenario do following Set up a simple list "simple_list" with Choice column "choice_col", allow multiple choices.

Then run following

Set-PnPListItem -List 'simple_list' -Identity 1 -Values @{choice_col=@("one", "two")} -Batch $batch

It will result in

A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth
is larger than the maximum allowed depth of 0

Then remove the Batch, and will work:

Set-PnPListItem -List 'simple_list' -Identity 1 -Values @{choice_col=@("one", "two")} 

mirontoli avatar May 14 '21 10:05 mirontoli

@gautamdsheth / @erwinvanhunen I can't reproduce the exact errors above, however I get the following when trying to use -batch with the Set-PnPListItem

A node of type 'StartArray' was read from the JSON reader when trying to read a value of a property; however, a 'PrimitiveValue' or 'StartObject' node was expected. with a ResponseCode: 400

Might need to transfer the issue?

veronicageek avatar May 24 '21 20:05 veronicageek

While the first two errors are both similar in verbiage describing circular references.

A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.

A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 0

The third one seems quite a bit different, so not sure if they are all related but they DO all happen with -Batch it seems. I'll say I was using Powershell 7.1, so the Core files were being called instead of the Framework ones. Not sure if it was the same for the other errors described here?

JAK1047 avatar May 26 '21 02:05 JAK1047

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] avatar Jun 09 '21 02:06 github-actions[bot]

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] avatar Jun 25 '21 02:06 github-actions[bot]

@JAK1047 - Please try with the -nightly tomorrow, or reopen if the issue persists. Thank you.

veronicageek avatar Jul 02 '21 15:07 veronicageek

I'm also having this issue and have tried the nightly version without any luck.

CodeWilliamson avatar Jul 14 '21 14:07 CodeWilliamson

Hi, I'am facing this issue too with latest version 1.7.0 Workaround via Graph might do the job: https://michalsacewicz.com/add-items-to-a-sharepoint-list-with-powershell-pnp-vs-batch-requests-to-graph/

cangot avatar Jul 28 '21 13:07 cangot

I'm getting the same issue

HudsonCFoxtel avatar Sep 15 '21 03:09 HudsonCFoxtel

I had the same issue when using -batch I was able to get it work when using a string. Seperate choices with ;#

example: $ownerUnits = "Choice1;#Choice2"

or if you have an array with strings: $ownerUnits -join ";#"

cosibox avatar Dec 18 '21 00:12 cosibox