0

I'm trying to set multiple values to lookup column. I can get it to set one value but multiple values don't work

see below

[Microsoft.SharePoint.SPFieldLookupValueCollection] $itemValues = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection

[Microsoft.SharePoint.SPFieldLookupValue] $lookupvalue = New-Object Microsoft.SharePoint.SPFieldLookupValue

$lookupvalue.LookupId = 1

$lookupvalue.LookupId = 2

$itemValues.Add($lookupvalue)

$CMRSItems["Event Type"] = $itemValues
$CMRSItems.Update()

I was expecting the LookupID 1 and 2 to update but it doesn't. When i specify LookupId = 1 it works

What am i doing wrong.

Thanks in Advance

updating multivalues

$lookupvalue = GetLookUpValues -val $number
#Write-Host $lookupvalue 

[Microsoft.SharePoint.SPFieldLookupValueCollection] $itemValues = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection
[Microsoft.SharePoint.SPFieldLookupValue] $lookupvalue = New-Object Microsoft.SharePoint.SPFieldLookupValue 


   #sample lookup values adding to collection: 
   #1;#Open
   #2;#Closed

$itemValues.Add($lookupvalue)

$CMRSItems["Event Type"] = $itemValues;
    $CMRSItems.Update();

Getting error : Exception calling "Update" with "0" argument(s): "Invalid look-up value

1
  • On your update: You create a collection, you add an object and assign the collection to the field. But nowhere do you set the LookupID on the object ;), which I think would be necessary. Commented Mar 14, 2014 at 9:15

2 Answers 2

1

You must create two objects of LookUpValue and add to the collection. Right now you are setting LookupID twice on the same object, overwriting the first assignment.

   [Microsoft.SharePoint.SPFieldLookupValueCollection] $itemValues = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection

  [Microsoft.SharePoint.SPFieldLookupValue]    $lookupvalue = New-Object Microsoft.SharePoint.SPFieldLookupValue

  [Microsoft.SharePoint.SPFieldLookupValue]    $lookupvalue2 = New-Object Microsoft.SharePoint.SPFieldLookupValue

$lookupvalue.LookupId = 1

$lookupvalue2.LookupId = 2

 $itemValues.Add($lookupvalue)
 $itemValues.Add($lookupvalue2)

Also in your new example you seem to overwrite your $lookupvalue

$lookupvalue = GetLookUpValues -val $number
#Write-Host $lookupvalue 

[Microsoft.SharePoint.SPFieldLookupValueCollection] $itemValues = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection
#[Microsoft.SharePoint.SPFieldLookupValue] $lookupvalue = New-Object Microsoft.SharePoint.SPFieldLookupValue #This row creates a new empty lookupvalue, overwriteing the one from line 1

$itemValues.Add($lookupvalue)

$CMRSItems["Event Type"] = $itemValues;
$CMRSItems.Update();
6
  • Thanks Rob just missed that..But when i try adding dynamic values to lookupid to update multi values i get an error. I'll post it soon
    – naijacoder
    Commented Mar 14, 2014 at 7:11
  • Robert I have added the code im using to update multi vakues to my original post.Thanks
    – naijacoder
    Commented Mar 14, 2014 at 7:16
  • Thanks Rob for the update.But its only updating the last value.I'm sending 3 values to update but only the last value is being updated.Any ideas
    – naijacoder
    Commented Mar 16, 2014 at 22:41
  • Also When i hard code the multivalues it works.:(
    – naijacoder
    Commented Mar 16, 2014 at 23:16
  • I do also get error:-Cannot convert value "Event Type" to type "System.Int32". Error: "Input string was not in a correct format." Some of the values im updating is sinple and some are mutli valued.Im sure i'm sending the right format
    – naijacoder
    Commented Mar 16, 2014 at 23:29
0

Add toString when updating the lookup field as shown below:

$CMRSItems["Event Type"] = $itemValues.ToString();

Your Answer

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

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