1

I would like to get the lookup Value from a List item depending on it's Title.

My List is set up similar to the following example:

ColumnA : a | b | c
lookupB : 1 | 1 | 2

The jquery:
function getValues(ListName)
{
$.getJSON('http://<pageName>/_vti_bin/ListData.svc/' + ListName + '?', function(data) { $.each(data.d.results, function(index, value) { $('#select').append ($('<option>/option>').val(index).html(value.Titel + value.lookupB)); });
});
}

The #select value is a <select> DropDown Menu and the options are filled correctly with the first value, but the lookup value only consists of [Object object] or undefined after trying different keys.

EDIT: I actually found out by looking into the Array of value.lookupB that I can add a value.lookupB.__deferred. The latter returns an uri pointing to:
http://<page>/_vti_bin/ListData.svc/ColumnA(1)/lookupB
Is there a way to access the information without parsing the JSON again?

Thank you.

2 Answers 2

2

I actually found the solution:

function getValues(listName)
{
    $.getJSON('http://<page>/_vti_bin/ListData.svc/' + listName,
        function(data) {
            $.each(data.d.results,
            function(index, value) {
                i++;
                $('#select').append($('<option></option>').val(index).html(value.ID + " : " + value.Titel));
                getLookup(value.ID, value.Titel);
            });
    });
}


function getLookup(idOfChild, valueOfChild)
{
    $.getJSON('http://<page>/_vti_bin/ListData.svc/' + childList + "(" + idOfChild + ")/" + parentList,
        function(data) {
            $.each(data,
            function(index, value) {
                $('#select2').append($('<option></option>').val(index).html(valueOfChild + " : " + value.Titel));
            });
    });
}

The first Dropdown is filled with the IDs and value.Titles from the child List and the second Dropdownlist is filled with value.Title of the child : corresponding value.Title parentlist. It works!
The key to the solution was actually as stated in the edit block of my question.

@ErikB: Thank you for your help, helped me bounce quite well.

1
  • This is an interesting solution, thanks for sharing
    – eirikb
    Commented Sep 12, 2012 at 17:54
1

How about something like this:

function getLookupFieldValuesFromListByTitle(listTitle, fieldInternalName, callback) {
  var ctx = new SP.ClientContext;
  var field = ctx.get_web().get_lists().getByTitle(listTitle).get_fields().getByInternalNameOrTitle(fieldInternalName);
  ctx.load(field);
  ctx.executeQueryAsync(function() {
    // Is not instanceof SP.FieldLookup
    var lookupListId = $(field.get_schemaXml()).attr('List');
    getItemsByListId(lookupListId, callback);
  }, function(a, b) {
    callback(b);
  });
}

function getItemsByListId(listId, callback) {
  var ctx = new SP.ClientContext;
  var items = ctx.get_web().get_lists().getById(listId).getItems(new SP.CamlQuery());
  ctx.load(items);
  ctx.executeQueryAsync(function() {
    var e = items.getEnumerator();
    var array = [];
    while (e.moveNext()) {
      array.push(e.get_current());
    }
    callback(null, array);
  }, function(a, b) {
    callback(b);
  });
}

It uses ClientContext to get all items from a given list and returns them in an array, so you can loop through them like this:

getLookupFieldValuesFromListByTitle('myList', 'myLookupField', function(err, items) {
  $.each(items, function(i, item) {
    $select.append($('<option>').text(item.get_title()).val(item.get_id()));
  });
});
8
  • Looking into your answer and doing some reading on javascript object model. Does not work of the bat, sadly even after plugging in the values. Thx for your fast response though. Commented Sep 11, 2012 at 14:30
  • 1
    I might be misunderstanding. You want to get all lookup values for a given field in a list? So you need list title and field title
    – eirikb
    Commented Sep 11, 2012 at 14:35
  • Yes that is correct. But I have only used the REST approach so far first time I am looking at the javascript object model. Bit lost to be honest, but trying to wrap my head around it. Commented Sep 11, 2012 at 14:47
  • 1
    Updated it to work for lookup, but it became quite horrible. Probably exists another much cleaner way to solve it
    – eirikb
    Commented Sep 11, 2012 at 14:51
  • 1
    Have you looked at the excellent SPServices by Marc D?
    – eirikb
    Commented Sep 12, 2012 at 8:04

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.