[SharePoint]javascript client object model 获取lookup 类型的field的值,包括user类型(单人或者多人)的值。how to get the multiple user type/lookup type field value by Javascript client object model

1. how to get value

 1 var context = new SP.ClientContext.get_current();
 2 var web = context.get_web();
 3 var list = web.get_lists().getByTitle(listTitle);
 4 var listItem = list.getItemById(1);   
 5 context.load(listItem);
 6 context.executeQueryAsync(
 7    function() {
 8        var lookupVals = listItem.get_item(fieldName); //get multi lookup value (SP.FieldLookupValue[])
 9        for(var i = 0;i < lookupVals.length;i++) {
10            console.log(lookupVals[i].get_lookupId()); //print Id
11            console.log(lookupVals[i].get_lookupValue()); //print Value
12        }
13    },
14    function(sender,args){
15        console.log(args.get_message());
16    }
17 );

2. how to update value

 1 var context = new SP.ClientContext.get_current();
 2 var web = context.get_web();
 3 var list = web.get_lists().getByTitle(listTitle);
 4 var listItem = list.getItemById(1);   
 5 
 6 var lookupVals = [];
 7 //set 1st Lookup value
 8 var lookupVal1 = new SP.FieldLookupValue();
 9 lookupVal1.set_lookupId(1);
10 lookupVals.push(lookupVal1);
11 //set 2nd Lookup value
12 var lookupVal2 = new SP.FieldLookupValue();
13 lookupVal2.set_lookupId(2);
14 lookupVals.push(lookupVal2);
15 
16 listItem.set_item(fieldName,lookupVals);
17 listItem.update();
18 
19 context.executeQueryAsync(
20    function() {
21         console.log('Multi lookup field has been updated');
22    },
23    function(sender,args){
24        console.log(args.get_message());
25    }
26 );

 

posted @ 2016-07-05 14:37  杨崇伟  阅读(552)  评论(0编辑  收藏  举报
版权所有,转载请标明出处