Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

onerror = handleError

function handleError() {
    return true;
}

can you try context.load(item)

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top