Pnp-powershell: Add-PnPListItem Managed Metadata Multiple Values - Object reference not set to an instance of an object.

Created on 3 Aug 2017  路  29Comments  路  Source: pnp/PnP-PowerShell

Attempting to add an item with a MMD Column that has multiple values throws an Object reference not set to an instance of an object error. This happens regardless of weather you use the path or term ids.

I've tested this on a list with a Content Type that has 1 column of type MMD, adding a single value works just fine

Example:
$Country = '"25a25ed2-41a4-4558-a6f6-8d2937727076","122b590b-ad93-46da-9a49-558feec7a0ce"'
Add-PnPListItem -List $DestinationList -ContentType "CDH Pitch Management" -Values @{"Country" = $Country}

Steps To Reproduce
Create a custom list with a content type that has 1 column of type MMD that allows multiple values.
Add an item to that list using the command.

This is on a SharePoint Online environment.

All 29 comments

Your array of country GUIDs are wrapped with ', so it's a string not an array. Just do $Country = "25a25ed2-41a4-4558-a6f6-8d2937727076","122b590b-ad93-46da-9a49-558feec7a0ce" without the wrapping single quotes

Yeah at first i didn't wrap them, but after that failed i wrapping them so that they resemble all the examples and that didn't go too well. Has the command been working for you? Maybe i'm doing something else wrong

Try this, try Add-PnPListItem without those values, just Title, which should return the list item, then use Set-PnPListItem, send all the values, with -SystemUpdate (so not to incur a new version or different modified time)

I forget if I got this working on Add-PnPListItem or not. I did fix the code for Set, and in this library each function is a different codeset soooo I don't think Add has been updated yet. LMK if that works and I'll get the Add updated as well.

Hi, did you manage to get this working? I'm having exactly the same problem inserting multiple MMD items using both Add and Set with SharePoint Online. I have tried all methods mentioned here but I keep getting 'Object reference not set to an instance of an object'.

Example:

    $values = @{ "SupplierName" = "Some Name"; } 
    $supplier = Add-PnPListItem -List $list -Values $values -ContentType $contentType

    $values = @{"ApprovalScope" = "d4a589c6-266a-48a3-b96e-0331ba3f1d89","5a94065a-51f1-48c0-8992-d36e05263bf6","7e007012-b749-4dee-96e1-45dcdeedfa33","96dc298e-1914-4c51-9e08-07877ecc347c"; }

    Set-PnPListItem -List $list -Identity $supplier.id -Values $values -ContentType $contentType

Unfortunately no, i added the missing values manually as i was already pressed for time. However i have another project that requires the same type of work so i guess i'll be trying to figure this out soon. I shamefully haven't tested the solution supplied DavidLozzi above, have you tried using this "SystemUpdate" approach?

I have, unfortunately with the same result.

@DavidLozzi I was wondering if you have had chance to look in to getting this working yet?

Many thanks.

Bump on this issue. I tried the suggested solution, although it does not give any error with Set-PnpListItem (as opposed to Add-PnpListItem), it does not seem to work either.

I have not found a way to set an MMD Field (mono or multi valued) with Pnp yet :s

The error I have with Add-PnpListItem when I try to set MMD fields:

Add-PnPListItem : Value cannot be null. Parameter name : path

I found the following on docs.microsoft.com:
Managed Metadata (multiple values with ids of terms): -Values @{"MetadataField" = ("fe40a95b-2144-4fa2-b82a-0b3d0299d818","52d88107-c2a8-4bf0-adfa-04bc2305b593")}

I have stopped trying to get it working with either Add-PnpListItem or Update-PnpListItem, it didn't work however I filled the value (string, array of multiple ids, array of a single id, Term path, etc).

According to your documentation, this is still an array, which I couldn't get to work (error on Add, no error but no action on Update)

However, i did manage to set my values with the Set-PnpTaxonomyFieldValue command, which allowed me (very very slowly) to update my item after creation and field by field:

Set-PnpTaxonomyFieldValue -ListItem $Item -InternalFieldName $MMDField -Terms @{ <term-guid> = "<term-label>", ... }

May be some can help me here.

I am trying to add values to a multi select Managed Metadata column through a variable using Add-PnPListItem. Everything seems to be correct and but when executed it does not adds values to MM column.

$Array = New-Object System.Collections.ArrayList
Connect-PnPOnline -Url https://ravi.sharepoint.com/sites/portal

$term1 = Get-PnPTaxonomyItem -TermPath "Intranet|Affiliates|T1"
$term2 = Get-PnPTaxonomyItem -TermPath "Intranet|Affiliates|T1"

$Array.Add('"'+$term1.Id+'"')
$Array.Add('"'+$term2.Id+'"')

[string]$a = $null
$a = $Array -join ","
Add-PnPListItem -List "Quicklinks" -ContentType Quicklink -Values @{"Title" = "Ravi Title"; "RelevantTo" = ($a)}

Works when i pass the GUID directly to the method.

Add-PnPListItem -List "Quicklinks" -Values @{"Title" = "Ravi Title"; "RelevantTo" = ("01cfe2dc-0dff-4172-a66d-183848202533","57bf2ff2-68b0-4c16-7fae-3d746f52038d")}

Any hints?

Thanks!
Ravi

Can't update using Set-PnPListItem. I've tried all type of synthax for the term but does not work. I need to update existing items using systemupdate so I can't use Set-PnpTaxonomyFieldValue because I need systemUpdate... It's been a long time without any fix.

The UpdateListItem method in ListItemHelper.cs checks if an array is passed in the value for a field of type "TaxonomyFieldTypeMulti". Hence an array has to be passed in the "Values" of the PowerShell command. An example shown below:

$listName = "Test list";
$pathPrefix = "Group Name|Term set name";

#EmptyArray
$taxValues = @(); 

$taxVal1 = $pathPrefix + "|Term 1"; #or the id of the term
#Add item to array
$taxValues += $taxVal1; 

$taxVal2 = $pathPrefix + "|Term 2"; #or the id of the term
#Add another item to array
$taxValues += $taxVal2;

#Pass the array in Values
Add-PnPListItem -List $listName -Values @{"Title" = "Test"; "Managed Metadata Field" = $taxValues}

I'll check this out, thanks!

I'm able to use Add-PnPListItem with managed metadata multiple values - but it won't work for columns that require a single value!

WARNING: You are trying to set multiple values in a single value field. Skipping values for field "Classification"

Here's how I have it working with both single and multiple values on columns that allow multiple values:
(in my case, I'm looping through term labels that are entered into a csv and looking up the Id field which I'm strongly typing as [string]$id... but the below was the key for building the array, whether there's one or more value doesn't matter)

[string[]]$values= $null; $values += $id

In the case of a column requiring a single value only, I've tried not passing it as an array, instead just as a string, but then I get this:
Add-PnPListItem : Value cannot be null.
Parameter name: taxValueCollection

Updating my last comment... I found I was setting my input as an array based on if I was sending multiple values, not based on if the field allowed multi-valued (array) input.

So for managed metadata using add-pnplistitem -values, I pass the following, and it works every time.

  • array of strings (even with just a single string/value) to attributes allowing multiple values
  • string to attributes not allowing multiple values.

I am trying to set MMD with PowerShell, and here is my script:

Connect-PnPOnline -Url https://mycompany.sharepoint.com/teams/ECAR-Migration/ -UseWebLogin

$DLAllIteams = Get-PnPListItem -List Projects

foreach ($DLSingleItem in $DLAllIteams) {

if ($DLSingleItem.FieldValues.Country -eq "Turkey") {

    Set-PnPListItem -list Projects -Identity $DLSingleItem.FieldValues.ID -Values @{"GeographicScope" = "09e8acd6-ecc4-4fc6-bf67-16e830fbe9ef"} 
}

}

I am receiving following error:

Set-PnPListItem : Value cannot be null.
Parameter name: taxValueCollection
At line:5 char:9

  • Set-PnPListItem -list Programmes -Identity $DLSingleItem.Fiel ...
  • ~~~~~~~~~~~~~

    • CategoryInfo : WriteError: (:) [Set-PnPListItem], ServerException

    • FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Lists.SetListItem

the version of PnP module is 3.8.1904.0

I am trying to set MMD with PowerShell, and here is my script:

Connect-PnPOnline -Url https://mycompany.sharepoint.com/teams/ECAR-Migration/ -UseWebLogin

$DLAllIteams = Get-PnPListItem -List Projects

foreach ($DLSingleItem in $DLAllIteams) {

if ($DLSingleItem.FieldValues.Country -eq "Turkey") {

    Set-PnPListItem -list Projects -Identity $DLSingleItem.FieldValues.ID -Values @{"GeographicScope" = "09e8acd6-ecc4-4fc6-bf67-16e830fbe9ef"} 
}

}

I am receiving following error:

Set-PnPListItem : Value cannot be null.
Parameter name: taxValueCollection
At line:5 char:9

  • Set-PnPListItem -list Programmes -Identity $DLSingleItem.Fiel ...
  •   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : WriteError: (:) [Set-PnPListItem], ServerException
    • FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Lists.SetListItem

the version of PnP module is 3.8.1904.0

@A298B112 - Does GeographicScope allow multiple values? If so, you must declare your input as an array, even if it's an array of one value. See multiple posts above for examples.

Now, should it automatically do that for you? Sure would be nice, but it doesn't.

@ianbruckner - yes it does allow multiple values and now I declare the variable as an array and it seems to be working. However, I get another error:

Set-PnPListItem : Value cannot be null.
Parameter name: path
At line:6 char:9

  • Set-PnPListItem -list Programmes -Identity $DLSingleItem.Fiel ...
  • ~~~~~~~~~~~~~

    • CategoryInfo : WriteError: (:) [Set-PnPListItem], ArgumentNullException

    • FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Lists.SetListItem

I mean where does it come from? Set-PnPListItem doesn't have a parameter -path

@A298B112 , I think is the path to the list "Programmes", check with "Lists/Programmes" !?

@PedroMordeP I am running this:

Set-PnPListItem -list "Lists/Programmes" -Identity $DLSingleItem.FieldValues.ID -Values @{"GeographicScope" = $taxValues}

It runs ok, without any errors, however, GeographicScope remains unchanged.

Here is script:

Connect-PnPOnline -Url https://mycompany.sharepoint.com/teams/mycompany-Migration/ -UseWebLogin

$DLAllIteams = Get-PnPListItem -List Programmes

[System.Collections.ArrayList]$taxValues = @();
$taxValues.Add("09e8acd6-ecc4-4fc6-bf67-16e830fbe9ef")

foreach ($DLSingleItem in $DLAllIteams) {

if ($DLSingleItem.FieldValues.Country -eq "Turkey") {

    Set-PnPListItem -list "Lists/Programmes" -Identity $DLSingleItem.FieldValues.ID -Values @{"GeographicScope" = $taxValues}
}

}

09e8acd6-ecc4-4fc6-bf67-16e830fbe9ef - is termset ID in Managed Metadate column

@A298B112
Try initializing the array like this

$taxValues = @("09e8acd6-ecc4-4fc6-bf67-16e830fbe9ef")

then add more values like $taxValues += "other-value"

@PedroMordeP I've tried like you suggested, no luck unfortunately.
the script runs without errors, however, column on a Document Library remains unchanged
Could it be due to rights on the Team Site? I can change the column manually if that matters.

I've done tests on this and if you use the path of the MMD like the example below should work

$userName = 'user'
$password = 'pw' | ConvertTo-SecureString -Force -AsPlainText
$cred = New-Object -typename System.Management.Automation.PSCredential($userName, $password)
$siteCollectionUrl = "https://contoso.sharepoint.com/"
Connect-PnPOnline $siteCollectionUrl -Credentials $cred 
$pathTermStore = "People|Department|Account"
$pathTermStore2 = "People|Department|Finance"
Add-PnPListItem -List "MMD" -Values @{"Multiple" = $pathTermStore,$pathTermStore2; "Title"= "blabla"}

MMD

This is working for me with single or multi values.
I create an array is multi value, and a string if single value

$termsWithPath = $null
if ($newTermsArray.Count -gt 1) {
    # If multi-value, create an array of terms
    $termsWithPath = @()
    foreach ($term in $newTermsArray) {
        $termsWithPath += $termPath + $newTerm
    }
}
else {
    # If single value
    $termsWithPath = $termPath + $newTerm
}

Set-PnPListItem -List $library -Identity $itemId -SystemUpdate -Values @{"$listColumn" = $termsWithPath }

-Values @{"$listColumn" = $termsWithPath }

@joelfmrodrigues can you please provide example with term id or path for your above poc to add multiple value in MMD field ?

@BhavikShingala Please check my code snippet above.
You basically pass an array if multi-value or a simple string if single value. An array with only one item will fail

Hi Folks,
I've spent 3 days to make it work. So my meds for this problem:

  • SharePointPnPPowerShellOnline 3.25.2009.1;
  • metadata fields could be filled only in Set-PnPListItem cmdlet. Add-PnPListItem cmdlet doesn't work and next bullet explains why;
  • one multiple values field could be set at once. IDK why but if you'll try to fill two or more multiple values field in one Add orSet cmdlet, the only last one in @values array will be filled;
  • if you have a single value only field you need to push string into @values;
  • if you have multiple values field but only one value to put in - use array @() with string values;
  • if you have multiple values field and multiple values to put in - array @() with string values as well.

I hope it could help somebody to use PNP or to fix PNP :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DanielSanIT picture DanielSanIT  路  59Comments

heinrich-ulbricht picture heinrich-ulbricht  路  14Comments

janegilring picture janegilring  路  23Comments

robbert-vanandel picture robbert-vanandel  路  29Comments

Forket picture Forket  路  39Comments