1

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
    "$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
    $itemValues.Add($lookupvalue)
    $CMRSItems["Event Type"] = $itemValues;
    #I can view the items returned successfully
    Write-Host "items:" $itemValues
    $CMRSItems.Update()
}

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

3
  • GetLookUpValues how does that method work? Commented Mar 17, 2014 at 7:02
  • Is this the same question as this: sharepoint.stackexchange.com/questions/92688/… ? Commented Mar 17, 2014 at 11:10
  • Robert this is how it works below ** GetLookUpValues this is how it works I pass an ID to list items and return data of type SPFieldLookupValue Commented Mar 17, 2014 at 11:24

1 Answer 1

1

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

EDIT:

#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.

$itemValues.Add($lookupvalue)
$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"]

3
  • Thanks Cameron.Yeah thats right but i was trying to use only the lookupid which i declared like this : $lookupvalue.LookupId = $number Commented Mar 17, 2014 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 Commented Mar 19, 2014 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. Commented Mar 19, 2014 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.