1

有问题的错误

Uncaught 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. 

这是代码,只是试图获取列表的简单计数。

var CustomAction = function(){

    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web(); 
    this.oList = web.get_lists().getByTitle("Classification");

    // .load() tells CSOM to load the properties of this object
    // multiple .load()s can be stacked
    clientContext.load(oList);

    // now start the asynchronous call and perform all commands
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure));

    // method will exit here and onSuccess or OnFail will be called asynchronously

};

function onSuccess(sender, args) {
    alert('No of rows: ' + oList.get_itemCount());
};

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

错误发生在 oList.get_itemCount()。发生这种情况的原因是什么?我尝试使用$( document).ready$(window).onload但问题仍然存在。所以就像我说的,当我将它复制/粘贴到浏览器中但从文件中运行它时它不起作用。

4

1 回答 1

1

试试这个

var CustomAction = function(){

    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web(); 
    this.oList = web.get_lists().getByTitle("Classification");

    // .load() tells CSOM to load the properties of this object
    // multiple .load()s can be stacked
    clientContext.load(oList);

    // now start the asynchronous call and perform all commands

    clientContext.executeQueryAsync((function(sender, args){alert('No of rows: ' + oList.get_itemCount())}(this, this.onSuccess)), (function(sender, args){alert('Request failed.\n' + args.get_message() + '\n' + args.get_stackTrace())}(this, this.onFailure)));
};
于 2014-10-30T17:06:05.160 回答