Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
132
rated 0 times [  136] [ 4]  / answers: 1 / hits: 21070  / 10 Years ago, thu, march 27, 2014, 12:00:00

Is there a way to edit a multivalue lookup field using the JavaScript Client Object Model?
I need to remove one or more lookup values and, eventually, add one or more values.



I search everywhere, I read MSDN documentation, ..., I also take a look under my desk!



Thanks.


More From » sharepoint

 Answers
73

Multiple-Column Lookup value is represented as an array of SP.FieldLookupValue objects.



How to read multiple Lookup field value



var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
var listItem = list.getItemById(1);
context.load(listItem);
context.executeQueryAsync(
function() {
var lookupVals = listItem.get_item(fieldName); //get multi lookup value (SP.FieldLookupValue[])
for(var i = 0;i < lookupVals.length;i++) {
console.log(lookupVals[i].get_lookupId()); //print Id
console.log(lookupVals[i].get_lookupValue()); //print Value
}
},
function(sender,args){
console.log(args.get_message());
}
);


How to update multiple Lookup field value



For updating multiple Lookup value you need to specify value of type SP.FieldLookupValue[]. Note, SP.FieldLookupValue could be initialized by specifying LookupId only.



var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
var listItem = list.getItemById(1);

var lookupVals = [];
//set 1st Lookup value
var lookupVal1 = new SP.FieldLookupValue();
lookupVal1.set_lookupId(1);
lookupVals.push(lookupVal1);
//set 2nd Lookup value
var lookupVal2 = new SP.FieldLookupValue();
lookupVal2.set_lookupId(2);
lookupVals.push(lookupVal2);

listItem.set_item(fieldName,lookupVals);
listItem.update();

context.executeQueryAsync(
function() {
console.log('Multi lookup field has been updated');
},
function(sender,args){
console.log(args.get_message());
}
);

[#71746] Wednesday, March 26, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
skyler

Total Points: 646
Total Questions: 119
Total Answers: 96

Location: Bonaire
Member since Wed, Mar 29, 2023
1 Year ago
;