0

I will add multi Lookup values:

var items = List.getItems(camlQuery);
clientContext.load(items);
clientContext.executeQueryAsync(
    function(sender, args) {
        var itemEnum = items.getEnumerator();

        // if we have an item, update it
        if (itemEnum.moveNext()) {
            // update the item
            var current = itemEnum.get_current();

            //update item lookup
            var lookups = [];

            for (var ii in lookupsIds) {

                var lookupValue = new SP.FieldLookupValue();
                lookupValue.set_lookupId(lookupsIds[ii]);
                lookups.push(lookupValue);

            }
            console.log(lookups);
            current.set_item('Category', lookups);
            current.update();
            clientContext.load(current);

output :

(3) [SP.FieldLookupValue, SP.FieldLookupValue, SP.FieldLookupValue] 0: SP.FieldLookupValue {$1s_1: "3"} 1: SP.FieldLookupValue {$1s_1: ","} 2: SP.FieldLookupValue {$1s_1: "4"} length: 3 proto: Array(0)

my problem, the values ​​of lookups (3,4) do not add in the list

1
  • 1
    Can you also paste the part of the code where you assign the value to the lookupsIds variable? Feb 12, 2019 at 16:23

1 Answer 1

0

Try using the following scripts:

<script src="https://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
var clientContext;
var collListItem;
$(document).ready(function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
        // the list name
        var listName = "cl03";                                  
        clientContext = new SP.ClientContext();
        var oList = clientContext.get_web().get_lists().getByTitle(listName);
        var camlQuery = new SP.CamlQuery();
        collListItem = oList.getItems(camlQuery);
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.updateLookups),
            Function.createDelegate(this, this.onQueryFailed)
        );                             
    });    
});

function updateLookups(sender, args) { 
    // lookup ids
    var lookupsIds = [3,4];
    var listItemEnumerator = collListItem.getEnumerator();
    // iterate items
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        var lookupVals = [];
        lookupsIds.forEach(function (id) {
            var lookupVal = new SP.FieldLookupValue();
            lookupVal.set_lookupId(id);
            lookupVals.push(lookupVal);
        });

        oListItem.set_item('Category',lookupVals);
        oListItem.update();
        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded),
            Function.createDelegate(this, this.onQueryFailed)
        );

    }

}
function onQuerySucceeded()  
{  
    console.log('Updated Successfully');  
}

function onQueryFailed(sender, args) {
    console.log('Request failed. ' + args.get_message() +
    '\n' + args.get_stackTrace());
}
</script>

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.