I've a custom list which contains a lookup field to a document library. I need to create a html table with for items in the custom list with the name of the linked field in the document library using JavaScript CSOM.
I tried the following code:
var context = SP.ClientContext.get_current();
var myList = context.get_web().get_lists().getByTitle("MyList");
var myDocLibrary = context.get_web().get_lists().getByTitle("MyDocLibrary");
var camlQuery = new SP.CamlQuery();
var camlQueryString =
'<View>\
<Query>\
<Where>\
<Eq>\
<FieldRef Name="MyLookupField" LookupId="True" />\
<Value Type="Lookup">1234</Value>\
</Eq>\
</Where>\
</Query>\
</View>';
camlQuery.set_viewXml(camlQueryString);
var items = myList.getItems(camlQuery);
context.load(items);context.executeQueryAsync(
onListItems,
function (sender, args){alert('Request failed. \nError: ' + args.get_message());}
);function onListItems() {
var enumerator = items.getEnumerator();
while(enumerator.moveNext()){
var currentItem = enumerator.get_current();
lookupField = currentItem.get_item("myLookupField");
lookupId = lookupField.get_lookupId();
currentDocument = myDocLibrary.getItemById(lookupId);context.load(currentDocument);
context.executeQueryAsync(
(function() {
return function() {
onListDocument(lookupId, currentDocument);
}
})(lookupId, currentDocument),
function(sender, args) {
alert('Request failed. \nError: ' + args.get_message());
}
);
}
}function onListDocument(lookupId, document) {
// Throws error
var documentName = document.get_item("FileLeafRef");
}
Unfortunately I receive an error "he property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested." if I try to read the FileLeafRef field.
Using a Include string in the load method doesn't help, in this case the query cannot be executed.
Any idea whats wrong with this?
Thanks
Pascal