SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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) {
  alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());


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. – bgmCoder 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? – bgmCoder 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
up vote 1 down vote accepted

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. – bgmCoder 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? – bgmCoder 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. – bgmCoder Oct 8 '12 at 19:18

Your Answer


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.