Salvete! How do I use javascript to fetch a value from a custom list that is not loaded in the browser?
This post seems relevant, but it fetches values from a list that is loaded.
From that post, I have created this, but I can't get it to show any alerts or log anything in the console. I can't say I understand exactly what is going on, however - delegates are shaky ground for me, and I am just learning the sp functions.
spload("getFields('Retreats', 'Capacity')"); //this is how I am calling the function
function spload(whatfunction){ //waits until sharepoint is ready
_spBodyOnLoadFunctionNames.push(whatfunction);
}
function getFields(listName, whatcolumn){
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var lists = web.get_lists();
//var listId = SP.ListOperation.Selection.getSelectedList();
//var list = lists.getById(listId);
var list = web.get_lists().getByTitle(listName);
var selectedItems = SP.ListOperation.Selection.getSelectedItems(); //<-- IS THIS RIGHT? If my list is not loaded, how can there be any selected items?
this.items = [];
for (var i in selectedItems) {
var id = selectedItems[i].id;
var item = list.getItemById(id);
items.push(item);
context.load(item, whatcolumn);
}
context.executeQueryAsync(Function.createDelegate(this, getAndShowTitle),
Function.createDelegate(this, showError));
}
function getAndShowTitle() {
for (var item in items) {
console.log(item.get_item(whatcolumn));
alert(item.get_item(whatcolumn));
}
}
function showError(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
[update]
Now, notice this line:
var selectedItems = SP.ListOperation.Selection.getSelectedItems();
If the list isn't loaded, how can there be any selected item?
ExecuteOrDelayUntilScriptLoaded(whatfunction, "sp.js");
in place of my own within my spload function, ` _spBodyOnLoadFunctionNames.push(whatfunction);`, but my codes don't fire.