Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to retrieve the selected values of multi select lookup column and store them in array and then set the lookup column with the same selected values after drop down value changes but always the value is returned as blank ... It works fine in case of single select look up column. Please help ........ I am really going mad with this issue :(

function getWebSiteData() 
{
    clientContext = new SP.ClientContext.get_current();
    if (this.clientContext != undefined && clientContext != null)
     {

            var webSite = clientContext.get_web();
            oList  = webSite.get_lists().getByTitle("Add New User");
            $.urlParam = function(name)
             {
                var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
                    return results[1] || 0;
                }
        var itemid = parseInt($.urlParam('ID'));  
        oListItem = oList.getItemById(itemid); 

        clientContext.load(oListItem,"ID","Unit","System");
        clientContext.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));                        
     }
}
function success(sender,args)
{
    **alert(oListItem.get_item("System").get_lookupId()); // this is null always**

    alert('success');
}


function failed(sender, args) {
alert("failed. Message:" + args.get_message());
}

Thanks for your update, I saved the values to array using js code but can you tell me how to assign array values back to the right hand side of Multi-select lookup column on change of a drop down value, this also happens without save button being clicked.

share|improve this question
add comment

2 Answers

The following code should work for you:

function success(sender,args)
    {
        var itemArray = new Array();
        itemArray = oListItem.get_item("System");

            for (var i = 0; i < itemArray.length; i++) {
                var lookupObject = itemArray[i];
               // get_lookupId() and get_lookupValue() can be used to get the id and the value of the lookup field.
               alert(lookupObject.get_lookupId() + ” ” + lookupObject.get_lookupValue());
             }
    }

More details here: http://kamilmka.wordpress.com/2012/01/10/ecmascript-client-object-model-get-the-values-from-a-multi-select-lookup-column-field/

UPDATE: This way, you wont be able to get those values on click of the add button as the values are not yet stored in the list item. These values are stored only when you click the save button of the form.

If you want to get the values on click of the add button, you can use simple jquery/javascript to fetch the values from the right side box and store it in a array.

The client object model will only be able to fetch the value if it is stored in the list item.

share|improve this answer
add comment

get_item() on a field that allows multiple values will return an array, so get_lookupId will be null. You'll need to do something like this:

for(var i = 0; i < oListItem.get_item("System").length; i++)
{
  alert(oListItem.get_item("System")[i].get_lookupId());
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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