1
votes

I try to retrieve the content of a lookup field in a sharepoint 2010 list. (myList) I do this by using sharepoint designer and a javascript.js file The java script works for most columns in my script.

But not foor lookup columns.

My column Lookup column is named Device for lookup columns values it results:

  [object Object]

including the brackets [ ], i'd like to know how to expand it to show the list content.

Here is my code to retrieve list data

    <script type="text/javascript">  
    function ViewItem()  {
    var context = new SP.ClientContext.get_current(); 
    var web = context.get_web();
    var list = web.get_lists().getByTitle('myList');
    var query = SP.CamlQuery.createAllItemsQuery();
    allItems = list.getItems(query);
    context.load(allItems, 'Include(Title,Device)');
    context.executeQueryAsync(Function.createDelegate(this, this.success),Function.createDelegate(this, this.failed));
    }
    function success() {
    var TextFiled = "";
    var ListEnumerator = this.allItems.getEnumerator();
    while(ListEnumerator.moveNext())
    {
    var currentItem = ListEnumerator.get_current();
    TextFiled += currentItem.get_item('Title') + '->' 
    TextFiled += String(currentItem.get_item('Device')) + '<-' 
    TextFiled += + '\n';
    }
    alert(TextFiled);
    }
    function failed(sender, args) {
    alert("failed. Message:" + args.get_message());
    }</script><a onclick="Javascript:ViewItem();" href="#">View my Items</a>

`

2

2 Answers

0
votes

you need to cast it as SP.FieldLookupValue like this:

SP.FieldLookupValue _value = listItem.FieldValues["Device"] as SP.FieldLookupValue;
var mylookupvalue= _value.LookupValue;

see Get value from lookfield field and SP.FieldLookupValue Properties

0
votes

You can read look up column data by specifying get_lookupId()[For ID] or get_lookupValue()[For Value]

// Load ClientContext & oWeb here..
this.oList = oWeb.get_lists().getByTitle("Documents");
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('');
this.oListItem = oList.getItems(camlQuery);
this.clientContext.load(this.oListItem);
this.clientContext.executeQueryAsync(
    Function.createDelegate(this, successGetListHandler),
    Function.createDelegate(this, errorGetListHandler)
);

function successGetListHandler() {
    var listItemInfo = '';
    var listItemEnumerator = this.oListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nTitle: ' + oListItem.get_item('FileLeafRef') + 
            '\nChecked out User: ' + oListItem.get_item('CheckoutUser');
    }

    jQuery('.checked-out-count').html('List Name is : ' + oList.get_title() + '\n ' + listItemInfo);
}
function errorGetListHandler() {
    alert("Request failed: " + arguments[1].get_message());
}

For more reference you can check good answer in this question.