我在JSOM SharePoint 2013上有一个常见错误。我有一个函数
function getTasks() {
var context = new SP.ClientContext(appWebUrl);
var web = context.get_web();
context.load(web);
var splist = web.get_lists().getByTitle("Gorevler");
var taskQuery = new SP.CamlQuery();
taskQuery.set_viewXml("<View><Query>" +
"<Where><Eq><FieldRef Name='AssignedTo' LookupId='True' /><Value Type='Integer'>" + currentUserId + "</Value></Eq></Where></Query>" +
"<ViewFields><FieldRef Name='Title' /><FieldRef Name='AssignedTo' /><FieldRef Name='StartDate' /><FieldRef Name='DueDate' /></ViewFields></View>");
var taskQueryItems = splist.getItems(taskQuery);
context.load(taskQueryItems);
context.executeQueryAsync(
Function.createDelegate(this, successHandler),
Function.createDelegate(this, errorHandler)
);
function successHandler() {
var taskAdet = taskQueryItems.get_count();
alert("Adet : " + taskAdet);
}
function errorHandler(sender, args) {
alert("Hata");
}
当我重新运行上面的代码时,它给了我:&#34;该集合尚未初始化。尚未请求或请求尚未执行。可能需要明确要求。&#34;为什么我必须刷新页面才能获得成功的结果?有没有办法在每次执行时获得正确的结果?
由于
答案 0 :(得分:0)
只需删除context.load(web)
。
function getTasks() {
var context = new SP.ClientContext(appWebUrl);
var web = context.get_web();
context.load(web); // <-- This is your problem.
var splist = web.get_lists().getByTitle("Gorevler");
var taskQuery = new SP.CamlQuery();
taskQuery.set_viewXml("<View><Query>" +
"<Where><Eq><FieldRef Name='AssignedTo' LookupId='True' /><Value Type='Integer'>" + currentUserId + "</Value></Eq></Where></Query>" +
"<ViewFields><FieldRef Name='Title' /><FieldRef Name='AssignedTo' /><FieldRef Name='StartDate' /><FieldRef Name='DueDate' /></ViewFields></View>");
var taskQueryItems = splist.getItems(taskQuery);
context.load(taskQueryItems);
context.executeQueryAsync(
Function.createDelegate(this, successHandler),
Function.createDelegate(this, errorHandler)
);
要:
function getTasks() {
var context = new SP.ClientContext(appWebUrl);
var splist = context.get_web().get_lists().getByTitle("Gorevler");
var taskQuery = new SP.CamlQuery();
taskQuery.set_viewXml("<View><Query>" +
"<Where><Eq><FieldRef Name='AssignedTo' LookupId='True' /><Value Type='Integer'>" + currentUserId + "</Value></Eq></Where></Query>" +
"<ViewFields><FieldRef Name='Title' /><FieldRef Name='AssignedTo' /><FieldRef Name='StartDate' /><FieldRef Name='DueDate' /></ViewFields></View>");
var taskQueryItems = splist.getItems(taskQuery);
context.load(taskQueryItems);
context.executeQueryAsync(
Function.createDelegate(this, successHandler),
Function.createDelegate(this, errorHandler)
);