【发布时间】:2015-03-12 06:12:30
【问题描述】:
我在检索多个列表时遇到问题。有没有办法可以使用 JSOM 在不同的列表中发出多个请求?谢谢。
【问题讨论】:
-
我也有同样的问题。我希望有人可以帮助我们。
-
这是我项目中的要求,我尝试搜索但所有这些都是通过 C# 进行的。
标签: sharepoint
我在检索多个列表时遇到问题。有没有办法可以使用 JSOM 在不同的列表中发出多个请求?谢谢。
【问题讨论】:
标签: sharepoint
老问题,但我想将这个答案发布给未来的读者。
只需获取所有列表,然后一个接一个地加载它们:
var ctx = SP.ClientContext.get_current();
// assuming you're working with the appweb
var list1 = ctx.get_web().get_lists().getByTitle("list1");
var list2 = ctx.get_web().get_lists().getByTitle("list2");
var list3 = ctx.get_web().get_lists().getByTitle("list3");
ctx.load(list1);
ctx.load(list2);
ctx.load(list3); // multiple loads required
ctx.executeQueryAsync( // one call to the server
function(){
// do something with the lists
},
function(){
// fail
});
在使用 SharePoint 的 JSOM api 时利用 jQuery 的承诺模式也是一个好主意,如下所示:
// extending the jQuery namespace for convenience
// if this is a bad idea performance wise or something
// I would like to hear from someone :)
$.extend({
execQAsync: function(ctx){
var dfd = $.Deferred();
ctx.executeQueryAsync(
function(sender,args){
dfd.resolve();
},
function(sender, args){
dfd.reject();
});
return dfd.Promise();
}
});
那么你可以这样去做:
$.execQAsync(ctx)
.done(function(){
// do something when the promise resolves
})
.fail(function(){arguments[1].get_message() });
见Optimal/preferred way to call 'SP.ClientContext.executeQueryAsync' in SharePoint
P.S Doublecheck lettercasings 等,我在小提琴中写了所有这些。
【讨论】: