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();

  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());
}
share|improve this question
your delay function is not right. The ExecuteOrDelayUntilScriptLoaded function will wait until the SP.js file(the client object model) is loaded before firing. The _spBodyOnLoadFunctionNames function is a holdover from SP2007 and will not work with client object model code. – Derek Gusoff Sep 19 '12 at 15:22
I didn't realize you commented here! I tried putting ExecuteOrDelayUntilScriptLoaded(whatfunction, "sp.js"); in place of my own within my spload function, ` _spBodyOnLoadFunctionNames.push(whatfunction);`, but my codes don't fire. – BGM Sep 20 '12 at 2:42
I have to go away for a week, but when I return I will revisit this topic! – BGM Sep 20 '12 at 2:42

1 Answer

The code you linked should do the trick with one small tweak:

replace this line:

var list = lists.getById(SP.ListOperation.Selection.getSelectedList()); 

with this:

var list = web.get_lists().getByTitle(listName); 

...where listName is the name of your list.

You also need to actually call your getFields function, so put this at the top:

ExecuteOrDelayUntilScriptLoaded(getFields, "sp.js");
share|improve this answer
Okay, I posted my new version based on your edit. Thank you for your help. – BGM Sep 19 '12 at 14:09
...and I edited my answer as well. You're welcome! – Derek Gusoff Sep 19 '12 at 14:15
I actually was using my own version of a delay, which I added to my code above. My spload function will call an alert without issue. Is ExecuteOrDelayUntilScriptLoaded a better approach? – BGM Sep 19 '12 at 14:35
yes, because it's made for the client object model. Your approach will work for init.js and the other 2007-era js files, but NOT SP.js, which is the client OM. – Derek Gusoff Sep 20 '12 at 23:15
But even though I am using 2010, if I use the 'ExecuteOrDelayUntilScriptLoaded' function instead of ExecuteOrDelayUntilScriptLoaded(whatfunction, "init.js");, my jquery-based css edits fail. – BGM Oct 8 '12 at 19:18
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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