Exception calling “Update” with “0” argument(s): “Invalid look-up value

Had this error when trying to update SharePoint SPFieldLookupValueCollection using PowerShell.

Just to share a small part of my PowerShell that DID NOT Work

$valueCol = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection;
 
 $groupValue.Split(",") | % {
 $lookupTitle = $_;
 $lookupItem = $lookupItems| ? {$_.Title -eq $lookupTitle};
 if($lookupItem -eq $null)
 {
 Write-Host -f Yellow "Unable to locate Lookup Item $lookupTitle."
 }
 else
 {
$val = New-Object Microsoft.SharePoint.SPFieldLookupValue($lookupItem.ID, $lookupTitle);
 Write-Host "`t`tLookup:" $val.LookupId " LookupValue:" $val.LookupValue
 $valueCol.Add($val);
 }
 }
 
 Write-Host "`tAdding Value" $valueCol;
 $existingItem["MultipleLookupColumnName"] = $valueCol;
 $existingItem.Update();

I thought it was easy. Turned out that this script WILL ONLY store the last value of the loop that you updated. That means, if you were to have Multiple Value of “A,B,C”, it stores only the C !

Resolution

It turns out that you just need to include the Type of the object before intializing a PowerShell Variable and it works perfectly fine.

[Microsoft.SharePoint.SPFieldLookupValueCollection]$valueCol = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection;

$groupValue.Split(",") | % {
$lookupTitle = $_;
$lookupItem = $lookupItems| ? {$_.Title -eq $lookupTitle};
if($lookupItem -eq $null)
{
Write-Host -f Yellow "Unable to locate Lookup Item $lookupTitle."
}
else
{
[Microsoft.SharePoint.SPFieldLookupValue]$val = New-Object Microsoft.SharePoint.SPFieldLookupValue($lookupItem.ID, $lookupTitle);
Write-Host "`t`tLookup:" $val.LookupId " LookupValue:" $val.LookupValue
$valueCol.Add($val);
}
}

Write-Host "`tAdding Value" $valueCol;
$existingItem["MultipleLookupColumnName"] = $valueCol;
$existingItem.Update();

So please dont forget to do that!

 

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code