0

Is there any way via PowerShell to add data to a multi-value Lookup column instead of overwriting it?

For example, the item already has stuff checked off in the Lookup column. Values 1 and 2 are already checked. But I want to add 3 without overwriting 1 and 2.

Here is a snip of my code:

foreach($row in $csvVariable)
{
$updateitem = $list.GetItems($query) | Where {$_["Entity Name"] -eq $row.AccountIdName}

$lookupfield = $list.Fields["Lookup"] -as [Microsoft.SharePoint.SPFieldLookup];
$lookuplist = $webDestination.Lists[[Guid]$lookupfield.LookupList]; 
$sourcefield =  $lookupfield.LookupField;            
$lookupitem = $lookuplist.Items | Where {$_["Title"] -eq $row.new_UpperTierIdName}
$lookupvalue = New-Object Microsoft.SharePoint.SPFieldLookupValue($lookupitem.ID,$lookupitem.ID.ToString());  

$updateitem["Lookup Column"] = $lookupvalue
$updateitem.Update()
}

The csv file is a many-to-many report that changes every day. I just want to be able to add values to the lookup column, not clear and re-set.

2

You'll need to read the existing items and store them in a variable. For multi-values fields you'll want to SPFieldLookupValueCollection instead of SPFieldLookupValue. It's essentially an array of SPFieldLookupValue objects. Once you have the existing values, just call the field's .Add() method to add the new entry.

Here's some samples using SPFieldLookupValueCollection. They're in C# but you should be able to translate them to PowerShell easily enough:

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlookupvaluecollection.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.