From 680b8bb0c3cd81609b9a62725b0df5e8bd9b809b Mon Sep 17 00:00:00 2001 From: Giacomo Pozzoni Date: Sat, 30 Nov 2024 18:01:45 +0100 Subject: [PATCH] Fix Set-PnPListItem not saving taxonomy values when using -Batch parameter --- src/Commands/Utilities/ListItemHelper.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Commands/Utilities/ListItemHelper.cs b/src/Commands/Utilities/ListItemHelper.cs index bd79c264f..792044cf7 100644 --- a/src/Commands/Utilities/ListItemHelper.cs +++ b/src/Commands/Utilities/ListItemHelper.cs @@ -423,21 +423,22 @@ public static Dictionary GetFieldValues(PnP.Core.Model.SharePoin var label = string.Empty; var itemId = Guid.Empty; - if (!Guid.TryParse(arrayItem as string, out termGuid)) + if (!Guid.TryParse(arrayItem?.ToString(), out termGuid)) { - var batchedTerm = batch.GetCachedTerm(termGuid.ToString()); + var batchedTerm = batch.GetCachedTerm(arrayItem?.ToString()); if (batchedTerm.key == null) { - taxonomyItem = clientContext.Site.GetTaxonomyItemByPath(arrayItem as string) as Term; + taxonomyItem = clientContext.Site.GetTaxonomyItemByPath(arrayItem?.ToString()) as Term; if (taxonomyItem == null) { - throw new PSInvalidOperationException($"Cannot find term {arrayItem}"); + throw new PSInvalidOperationException($"Cannot find term '{arrayItem}'"); } var labelResult = taxonomyItem.GetDefaultLabel(defaultLanguage); clientContext.ExecuteQueryRetry(); label = labelResult.Value; itemId = taxonomyItem.Id; - batch.CacheTerm(termGuid.ToString(), termGuid, label); + batch.CacheTerm(arrayItem?.ToString(), itemId, label); + batch.CacheTerm(itemId.ToString(), itemId, label); } else { @@ -467,10 +468,12 @@ public static Dictionary GetFieldValues(PnP.Core.Model.SharePoin itemId = batchedTerm.id; label = batchedTerm.label; } - fieldValueCollection.Values.Add(field.NewFieldTaxonomyValue(itemId, label)); } - item[key as string] = fieldValueCollection; + + fieldValueCollection.Values.Add(field.NewFieldTaxonomyValue(itemId, label)); } + + item[key as string] = fieldValueCollection; } else {