Sharepoint Online executeQueryAsync JSOM - 返回值

时间:2015-07-02 13:23:21

标签: javascript sharepoint

使用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
});

警报(&#39;没有什么&#39); &lt; ---- quotedetails此时未定义 }

希望你能帮忙!

1 个答案:

答案 0 :(得分:1)

一些建议

  • 确保您使用jQuery版本&gt; = 1.5 ,因为Deferred对象 是在jQuery 1.5中引入的
  • 没有需要同时指定CAML ViewFieldsSP.ClientContext.load函数的第二个参数值,因为它们都是用于指定要检索的属性
  • 总是希望为SP.ClientContext.executeQueryAsync函数添加失败的回调,以便处理任何发生的错误

话虽如此,我建议您使用以下更通用的方法来获取列表项:

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

});