Sharepoint Online executeQueryAsync JSOM - Return Value
Sharepoint Online executeQueryAsync JSOM - Return Value
使用Sharepoint Online 2013使用JSOM构建应用程序。当前部分是在将数据提交到列表(工作正常(后,将打开一个模式,允许用户从该列表和其他列表中创建PDF。目前的问题是,在我开始将列表中的数据制作成PDF之前,必须先在客户端准备好该列表的数据。我已经阅读了关于"Promise"方法的文献,这也是我最近尝试的方法,但收效甚微。
function getQuoteDetails() {
var d = $.Deferred(function(){
var ListName = "Quote";
var context = new SP.ClientContext.get_current();
var lstObject = context.get_web().get_lists().getByTitle(ListName);
var camlQuery = new SP.CamlQuery();
//var _valuetofind = $('#QuoteID').text();
var _valuetofind = '1';
camlQuery.set_viewXml("<View><ViewFields>" +
"<FieldRef Name='Q_ID' />" +
"<FieldRef Name='CPY_ID' />" +
"<FieldRef Name='CUST_ID' />" +
"<FieldRef Name='Q_RaiseDate' />" +
"<FieldRef Name='Q_DueDate' />" +
"<FieldRef Name='Q_Price' />" +
"<FieldRef Name='Q_GST' />" +
"<FieldRef Name='Q_TotalPrice' />" +
"<FieldRef Name='Q_PurchaseOrder' />" +
"</ViewFields>" +
"<Query><Where><Contains>" +
"<FieldRef Name='Q_ID'/>" +
"<Value Type='Number'>" + _valuetofind + "</Value></Contains></Where></Query></View>");
var listItemCollection = lstObject.getItems(camlQuery);
context.load(listItemCollection, "Include(Q_ID, CPY_ID, CUST_ID, Q_RaiseDate, Q_DueDate, Q_Price, Q_GST, Q_TotalPrice, Q_PurchaseOrder)");
context.executeQueryAsync(onGetItemsSuccess, onGetItemsFail);
function onGetItemsSuccess(sender, args) {
var listItemEnumerator = listItemCollection.getEnumerator();
while (listItemEnumerator.moveNext()) {
var quotearray = [];
quotearray['Q_ID'] = listItemEnumerator.get_current().get_item('Q_ID');
quotearray['CPY_ID'] = listItemEnumerator.get_current().get_item('CPY_ID');
quotearray['CUST_ID'] = listItemEnumerator.get_current().get_item('CUST_ID');
quotearray['Q_RasiedDate'] = listItemEnumerator.get_current().get_item('Q_RasiedDate');
quotearray['Q_DueDate'] = listItemEnumerator.get_current().get_item('Q_DueDate');
quotearray['Q_Price'] = listItemEnumerator.get_current().get_item('Q_Price');
quotearray['Q_GST'] = listItemEnumerator.get_current().get_item('Q_GST');
quotearray['Q_TotalPrice'] = listItemEnumerator.get_current().get_item('Q_TotalPrice');
quotearray['Q_PurchaseOrder'] = listItemEnumerator.get_current().get_item('Q_PurchaseOrder');
}
d.resolve(quotearray);
}
function onGetItemsFail(sender, args) {
alert('Failed to get items. Error: ' + args.get_message());
}
});
return d.promise();
}
此代码由以下人员调用:
function CreateQuotePDF() {
getQuoteDetails().done(function (quotearray) {
var quotedetails = quotearray;
alert('Nothing'); <---- quotedetails is undefined at this point
});
alert("Nothing"(<----quotedetails在这一点上是未定义的}
希望你能帮忙!
一些建议
- 确保您使用的是jQuery版本>=1.5,因为
Deferred
对象在jQuery 1.5中引入 - 没有需要同时指定CAML
ViewFields
和SP.ClientContext.load
函数的第二个参数的值,因为它们都用于指定要检索的属性 - 始终倾向于包含SP.ClientContext执行QueryAsync函数的失败回调,以便处理任何发生的错误
话虽如此,我建议您使用以下更通用的方法来获取列表项:
function getListItems(listTitle,propertiesToInclude)
{
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle(listTitle);
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
var includeExpr = 'Include(' + propertiesToInclude.join(',') + ')';
ctx.load(items,includeExpr);
var d = $.Deferred();
ctx.executeQueryAsync(function() {
var result = items.get_data().map(function(i){
return i.get_fieldValues();
});
d.resolve(result);
},
function(sender,args){
d.reject(args);
});
return d.promise();
}
在这种情况下,可以检索列表项,如下所示:
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
var listTitle = 'Documents'; //set list title here
var properties = ['Title','ID']; //specify items properties here
getListItems(listTitle,properties)
.done(function(items){
console.log(items);
})
.fail(function(error){
console.log(error.get_message()); //if any error is occurred?
});
});
相关文章:
- 在ember/handlers中使用value和valueBinding有什么区别
- jquery if语句返回return wong语句
- 函数在return语句之前返回空对象
- Javascript Return and if/else
- 需要url导航从DropDown Value onClick按钮传递url变量
- Fizz Buzz return value javascript
- 为什么我需要写“;function(value){return my_function(value);作为node.js
- Return <td> value via onclick()
- How does jQuery's .not() return value affect .hide()?
- Javascript & jquery return value
- Jscript CRM Return Value
- document.getElementById().value return undefined in chrome
- return value from onClick
- Meteor.WrapAsync don't return value
- AngularJS http return value
- return object if list<object> value is exists (Javascr
- javascript innerhtml return value
- Java Script Return Value
- Sharepoint Online executeQueryAsync JSOM - Return Value
- 通过AJAX在JavaScript中生成一个变量return Value