I have an issue updating multi-values in a lookup field via powershell

I can update a single value lookup field as below but can't get to update if its multi- value.

As stated below when I hardcode it. It works.

No idea what 'm missing. Any help will be appreciated.

$array = @($realval.Split(';'))
for ($i = 0; $i -lt $array.Count - 1; $i += 2)
    $word = $array[$i].Trim('#')
    $number = $array[$i+1].Trim('#')
    "$number $word"
    #send param to GetLoolValues func to return records as SPFieldLookupValue
    $lookupvalue1 = GetLookUpValues -val $number
    #I can view the lookupvalue returned successfully
    #Write-Host $lookupvalue1
    [Microsoft.SharePoint.SPFieldLookupValueCollection] $itemValues = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection
    #This LookupId returns 3 values like so :1,2,6
    $lookupvalue.LookupId = $number
    $CMRSItems["Event Type"] = $itemValues;
    #I can view the items returned successfully
    Write-Host "items:" $itemValues

** GetLookUpValues this is how it works I pass an ID to list items and return data of type SPFieldLookupValue


In your listed code, you declare the variable $lookupvalue1 but further down you use $lookupvalue (missing the '1'). I'm assuming you changed this and forgot about it and it is still working without throwing errors because it's using an older object.

To help prevent such mistakes, you could enable strict mode by adding this at the top of your script:

Set-Strictmode -Version Latest


#This LookupId returns 3 values like so :1,2,6
$lookupvalue.LookupId = $number

I don't see how it does. LookupId is an int. It contains a single value. As such you must assign a single value. There's no way it can return 3 values.

$CMRSItems["Event Type"] = $itemValues;

Seeing this, you started out correctly, but adding item X to collection Y and assigning collection Y to your field, Z amount of times still only leaves one value in your field, not Z values, because it was overwritten each time.

What I think you want to do is loop as you are now, but keep adding the $lookupvalue to $itemValues each time and below your loop, assign $itemValues to $CMRSItems["Event Type"]

  • Thanks Cameron.Yeah thats right but i was trying to use only the lookupid which i declared like this : $lookupvalue.LookupId = $number – naijacoder Mar 17 '14 at 22:58
  • **Thanks Cameron for the tip. But find my problem as I was initiating the SPLookupvaluecollection in the loop. as soon as I took it out it working as expected. Cheer – naijacoder Mar 19 '14 at 0:40
  • 1
    Your code would still be inefficient as you're assigning the current collection each time in your loop, while doing it once in the end would be sufficient, as I said in my answer. – Cameron Verhelst Mar 19 '14 at 6:51

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.