Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I try to get the value of a list item (that is currently selected). I know that I must load it into the context but even with loading the field explicitly I get the following message.

Microsoft JScript runtime error: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

Here is what I used to test if I can get to the field values:

function showTitles(urlColumnName) {
    var selectedItemIds = getSelectedItemIds();
    for (var ii = 0; ii < selectedItemIds.length; ii++) {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var lists = web.get_lists();
        var list = lists.getById(SP.ListOperation.Selection.getSelectedList());
        var item = list.getItemById(ii);

        context.load(item, "Title");
        context.executeQueryAsync(Function.createDelegate(this, getAndShowTitle(item, urlColumnName)),
            Function.createDelegate(this, showError));
    }
}

function getAndShowTitle(item, urlColumnName) {
    var title = item.get_item("Title");
    alert(title);
}


function showError(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

It even fails with "Title". (UrlColumnName is not really used in this example). The functions are all in the same JS file and I don't use any global vars (for the item storage) but besides from that I don't see any difference to some working examples I found on the web.

The exception occurs on: var title = item.get_item("Title");

Any ideas what I'm missing?

share|improve this question
    
How many items you have on your list (total)? In general use case, how many ID's would you be querying? –  Jussi Palo Feb 14 '12 at 14:21

3 Answers 3

up vote 10 down vote accepted

You load multiple items. A conflict occurs. Collect all items you need and call "load". After you call load for all items, then run context.executeQueryAsync.

Save your selected items as a global variable (this.items):

function showTitles(urlColumnName) {
  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 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, "Title");
  }
  context.executeQueryAsync(Function.createDelegate(this, getAndShowTitle),
    Function.createDelegate(this, showError));
}
function getAndShowTitle() {
    for (var item in items) {
        console.log(item.get_item("Title"));
    }
}
function showError(sender, args) {
  alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
share|improve this answer
    
Ah, I see. I'll check that.. as soon as the guy outside my office has stopped his annoying laughter :-) –  Shihan Feb 14 '12 at 16:09
    
Allright. I hope the annoying guy will go to some other place and laugh :) –  Anatoly Mironov Feb 14 '12 at 16:16

If your JavaScript functionality is working fine, just handle the error in your .js file:

onerror = handleError

function handleError() {
    return true;
}
share|improve this answer

can you try context.load(item)

and then get the data for each field using item.get_item("Title") ?

share|improve this answer
    
That didn't work –  Shihan Feb 14 '12 at 16:06

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.