Query multiple SharePoint lists Using REST API and angular JS -


i have scenario of fetching data multiple sharepoint 2013 lists using rest api , angularjs. able fetch data 1 of sharepoint list requirements fetch data multiple lists on page load. using provider hosted app fetch data host web. have 2 methods calling 2 separate lists. i getting results first method when second method called after execution of 1st method. getting time out error. seems cannot call 2 methods 1 after other. below code, please me if missing or if there other way fetch data multiple sharepoint lists.

method 1: fetch data list 1

var query = listendpoint + "/getbytitle('candidatelist')/items?$select=id,firstname,middleinitial,lastname,emailaddress,primaryphoneno,profileimage,address,state,country,currenttitle,currentcompany,lastactivitymodifiedby,lastactivitymodifieddate,deletedstatus&@target='" + hostweburl + "'";      var getcandidates = function (query, querycandidatenotes)                      {                     alert('getrequest');                     var scriptbase = hostweburl + "/_layouts/15/";                     var deferred = $q.defer();                     // load 15hives js files , continue successhandler                         $.getscript(scriptbase + "sp.runtime.js",                         function () {`enter code here`                             $.getscript(scriptbase + "sp.js",                                 function () {                                     $.getscript(scriptbase +"sp.requestexecutor.js",                                          function () {                                              var executor = new sp.requestexecutor(appweburl);                                              executor.executeasync({                                                  url: query,                                                  method: "get",                                                  headers: { "accept": "application/json; odata=verbose" },                                                  success: successhandler,                                                  error: errorhandler                                              });                                              //deferred.resolve();                                          });                                 });                         });                      function successhandler(data) {                         var jsonobject1 = json.parse(data.body);                          deferred.resolve(jsonobject1);                      }                      function errorhandler(data, errorcode, errormessage) {                         alert('error1:' + errormessage + data.body);                     }                     //                      return deferred.promise;                      //candidate details ends                 }; 

method 2: fetch data list 2

    var querycandidatenotes = listendpoint + "/getbytitle('candidatenotes')/items?$select=title,candidateid&@target='" + hostweburl + "'";   // candidate notes             var getcandidatenotes = function (querycandidatenotes) {                 alert('getcandidatenotesrequest');                 var scriptbase = hostweburl + "/_layouts/15/";                 var deferred2 = $q.defer();                 // load 15hives js files , continue successhandler                     $.getscript(scriptbase + "sp.runtime.js",                     function () {                         $.getscript(scriptbase + "sp.js",                             function () {                                 $.getscript(scriptbase + "sp.requestexecutor.js",                                      function () {                                          var executor = new sp.requestexecutor(appweburl);                                          executor.executeasync({                                              url: querycandidatenotes,                                              method: "get",                                              headers: { "accept": "application/json; odata=verbose" },                                              success: successhandler,                                              error: errorhandler                                          });                                          //deferred.resolve();                                      });                             });                     });                  function successhandler(data) {                     var jsonobject2 = json.parse(data.body);                     //var results2 = jsonobject2.d.results;                     deferred2.resolve(jsonobject2);                     //alert('2nd success:' + jsonobject2);                     //return jsonobject2;                 }                  function errorhandler(data, errorcode, errormessage) {                     alert('error2 :' + errormessage + data.body);                 }                 //                  return deferred2.promise; 

};

method 3: calling method 2 after method 1

   var getrequest = function (query, querycandidatenotes) {                   var deferred = $q.defer();                 $.when(getcandidates(query, querycandidatenotes)).then(function (data) {                      alert('success1:' + data);                                             $.when(getcandidatenotes(querycandidatenotes)).then(function (data1) {                         deferred.resolve(data);                         alert('success2:' + data1);                     });                     })                 return deferred.promise;             };              return {                 getrequest: getrequest              };          }]); })(); 

$.when not appropriate here, utilize $q.all combines multiple promises single promise resolved when of input promises resolved.

example

app.controller('listcontroller', function ($scope, $q, listservice) {     sp.sod.executefunc('sp.requestexecutor.js', 'sp.requestexecutor', function () {          $q.all([listservice.getlistitems('documents'), listservice.getlistitems('site pages')]).then(function (data) {             $scope.documentsitems = data[0].d.results;             $scope.sitepagesitems = data[1].d.results;          });      }); }); 

where listservice service getting list items:

app.factory('listservice', ['$q', function ($q) {     var getlistitems = function (listtitle) {         var d = $q.defer();         jsrequest.ensuresetup();         var hostweburl = decodeuricomponent(jsrequest.querystring["sphosturl"]);         var appweburl = decodeuricomponent(jsrequest.querystring["spappweburl"]);          var queryurl = appweburl + "/_api/sp.appcontextsite(@target)/web/lists/getbytitle('" + listtitle + "')/items?@target='" + hostweburl + "'";         var executor = new sp.requestexecutor(appweburl);         executor.executeasync({             url: queryurl,             method: "get",             headers: { "accept": "application/json; odata=verbose" },             success: function(data, textstatus, xhr) {                 d.resolve(json.parse(data.body));             },             error: function(xhr, textstatus, errorthrown) {                 d.reject(json.parse(xhr.body).error);             }         });         return d.promise;     };      return {         getlistitems: getlistitems     }; }]); 

solution description

enter image description here

default.aspx

<asp:content contentplaceholderid="placeholderadditionalpagehead" runat="server">     <script type="text/javascript" src="../scripts/jquery-1.9.1.min.js"></script>     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>      <sharepoint:scriptlink name="sp.js" runat="server" ondemand="true" loadafterui="true" localizable="false" />     <meta name="webpartpageexpansion" content="full" />      <!-- add css styles following file -->     <link rel="stylesheet" type="text/css" href="../content/app.css" />      <!-- add javascript following file -->      <script type="text/javascript" src="../scripts/listservice.js"></script>     <script type="text/javascript" src="../scripts/app.js"></script>  </asp:content> 

and

<asp:content contentplaceholderid="placeholdermain" runat="server">     <div ng-app="spapp" ng-controller="listcontroller">     </div> </asp:content> 

app.js

'use strict';  (function() {      var app = angular.module('spapp', ['spapp.services']);      app.controller('listcontroller', function ($scope, $q, listservice) {          executeonsploaded(function () {                 $q.all([listservice.getlistitems('documents'), listservice.getlistitems('site pages')]).then(function (data) {                     $scope.documentsitems = data[0].d.results;                     $scope.sitepagesitems = data[1].d.results;                 });         });       });  })();    function executeonsploaded(loaded) {     jsrequest.ensuresetup();     var hostweburl = decodeuricomponent(jsrequest.querystring["sphosturl"]);     var scriptbase = hostweburl + "/_layouts/15/";     $.when(         //$.getscript(scriptbase + "sp.runtime.js"),         $.getscript(scriptbase + "sp.js"),         $.getscript(scriptbase + "sp.requestexecutor.js"),         $.deferred(function (deferred) {             $(deferred.resolve);         })     ).done(function () {         loaded();     }); } 

listservice.js

'use strict';   angular.module('spapp.services',[]) .factory('listservice', ['$q', function ($q) {     var getlistitems = function (listtitle) {         var d = $q.defer();         jsrequest.ensuresetup();         var hostweburl = decodeuricomponent(jsrequest.querystring["sphosturl"]);         var appweburl = decodeuricomponent(jsrequest.querystring["spappweburl"]);          var queryurl = appweburl + "/_api/sp.appcontextsite(@target)/web/lists/getbytitle('" + listtitle + "')/items?@target='" + hostweburl + "'";         var executor = new sp.requestexecutor(appweburl);         executor.executeasync({             url: queryurl,             method: "get",             headers: { "accept": "application/json; odata=verbose" },             success: function(data, textstatus, xhr) {                 d.resolve(json.parse(data.body));             },             error: function(xhr, textstatus, errorthrown) {                 d.reject(json.parse(xhr.body).error);             }         });         return d.promise;     };      return {         getlistitems: getlistitems     }; }]); 

Comments

Post a Comment

Popular posts from this blog

Capture and play voice with Asterisk ARI -

python - How to use elasticsearch.helpers.streaming_bulk -