Take the 2-minute tour ×

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

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);
    context.load(item, whatcolumn);
  context.executeQueryAsync(Function.createDelegate(this, getAndShowTitle),
    Function.createDelegate(this, showError));
function getAndShowTitle() {
    for (var item in items) {
function showError(sender, args) {


Now, notice this line:

 var selectedItems = SP.ListOperation.Selection.getSelectedItems(); 

If the list isn't loaded, how can there be any selected item?

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
Okay, I'm back to this: I've got the function firing, but can't get the listname right. My list is in the same site as the one from whence I am trying to fetch data, but it says, `List '/teamsite/mylist' does not exist at site. How should I specify the list name? –  BGM Jul 20 '13 at 22:14
edit your post to show your code as you have it now. Your getListByTitle() should get the title of the list, not the url or siteName/listName, just the list title. –  Derek Gusoff Jul 21 '13 at 2:04

1 Answer 1

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

Your Answer


