10

Related: How to retrieve a ListItem using the Javascript Client Object Model?

In my previous question I was able to successfully retrieve a basic value from a list item. My problem is that I am now trying to retrieve a value from a LookUp column get_item no longer seems to work.

When I do an alert, I get [object Object] instead of the value that is in there.

Code I am using:

function onSuccess(sender, args) {
        alert(oDeptItem.get_item('Department'));
        $('#contact').html(oDeptItem.get_item('Department'));
    }

Should I be using something besides get_item to values from LookUp columns?

  • get_lookupValue() will also work with fields of Type "User" – Vardhaman Deshpande Aug 30 '11 at 5:59
  • 1
    What about columns that belongs to the lookup field? For example Department name, Department No and so on. – Rob Jan 20 '14 at 12:50
18

Looks like I just needed to add the get_lookupValue() function after my get_item call:

alert(oDeptItem.get_item('Department').get_lookupValue());

Hope this helps someone...

1

For those not using JSOM and need a solution using REST + JSON, this can be done using the REST API and OData. For example if the list in questions is Employee with a Department Lookup field. We can send a REST request with this format:

_api/web/lists/getbytitle('employee')/items?$select=Department/Name,DepartmentId,Name,Id&$expand=Department

Note the OData request parameters $select and $expand (this is needed to get other fields from the lookup list). This request should return:

Employee id and name Department id and name

More information can be found here:

http://www.ozkary.com/2015/10/sharepoint-2013-api-odata-for-lookup-fields.html

hope it helps.

0

Using Abe Misseler answer:

var oItem ='';
function retrieveWebSite() {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
        var clientContext = new SP.ClientContext.get_current();
        this.oWebsite = clientContext.get_web();
        clientContext.load(this.oWebsite);

        var lstObject = oWebsite.get_lists().getByTitle('Listname');
        oItem = lstObject.getItemById(5);
        clientContext.load(oItem);

        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded), 
            Function.createDelegate(this, this.onQueryFailed)       
        );
    });
}

function onQuerySucceeded(sender, args) {
    var look  = oItem.get_item('LookupColumnName').get_lookupValue();
    var title = oItem.get_item('Title');
    var id = oItem.get_id();
    alert("Loook up column value: "+look);
    alert("Title column: "+title);
    alert("Id column value: "+id);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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