0

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?

4
  • 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. Commented Sep 19, 2012 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. Commented Sep 20, 2012 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? Commented Jul 20, 2013 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. Commented Jul 21, 2013 at 2:04

1 Answer 1

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");
10
  • Okay, I posted my new version based on your edit. Thank you for your help. Commented Sep 19, 2012 at 14:09
  • ...and I edited my answer as well. You're welcome! Commented Sep 19, 2012 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? Commented Sep 19, 2012 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. Commented Sep 20, 2012 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. Commented Oct 8, 2012 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.