# Issue with executing multiple executeQueryAsync is JSOM

I am trying to create a web part in SP 2016 where the web part contains 2 custom properties, site URL and calendar name (list name) present in that URL. I am trying to read the 2 properties, load the calendar present in the site URL and read all the events for the current week present in the calendar.

I have created the custom properties, able to read the properties in JSOM which is a promise. executeQueryAsync is not working which is a success call back of the getSiteUrl promise.

This is what I have right now.

    function getSiteUrl() {  //This fn reads the 2 custom properties
var dfd = $.Deferred(function () { var context = SP.ClientContext.get_current(); var web = context.get_web().getFileByServerRelativeUrl(_spPageContextInfo.serverRequestPath); var webPartManager = web.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared); var webPartDefs = webPartManager.get_webParts(); context.load(webPartDefs, 'Include(WebPart.Properties)'); context.executeQueryAsync( function () { // Successfull var site = {}; for (var i = 0; i < webPartDefs.get_count() ; i++) { var webPartDef = webPartDefs.getItemAtIndex(i); var webPart = webPartDef.get_webPart(); var properties = webPart.get_properties(); if (properties.get_fieldValues()['Title'] == 'DAIDS Calendar') { site.SiteFullUrl = properties.get_fieldValues()['SiteFullUrl']; site.calendarName = properties.get_fieldValues()['calendarName']; } } dfd.resolve(site); }, function (sender, args) { // Failure dfd.reject(args.get_message()); }); }); return dfd.promise(); } function intialize() { var siteUrl = getSiteUrl(); siteUrl.done(function (site) {// site has the 2 custom properties var request = {}; request.calendarName = site.calendarName; request.siteUrl = site.SiteFullUrl; var ctx = new SP.ClientContext(request.siteUrl); var list = ctx.get_web().get_lists().getByTitle(request.calendarName); request.query = new SP.CamlQuery(); request.query.set_viewXml('<View><Query><Where><DateRangesOverlap>' + '<FieldRef Name=\"EventDate\">' + '</FieldRef><FieldRef Name=\"EndDate\"></FieldRef>' +'<FieldRef Name=\"RecurrenceID\"></FieldRef><Value Type=\"DateTime\">' + '<Week/></Value></DateRangesOverlap>' + '</Where></Query></View>'); request.itemCollection = list.getItems(request.query); ctx.load(request.itemCollection, 'Include(Title,EventDate, Description, EndDate)'); console.log("Testing..."); //This is getting printed out. ctx.executeQueryAsync(function () { var listEnumerator = request.itemCollection.getEnumerator(); var calendarEvents = []; while (listEnumerator.moveNext()) { var oListItem = listEnumerator.get_current(); calendarEvents.push({ Title: oListItem.get_item('Title'), EventDate: oListItem.get_item('EventDate'), Description: oListItem.get_item('Description'), EndDate: oListItem.get_item('EndDate') }); }); } console.log(JSON.stringify($scope.calendarEvents));

},
function (sender, args) {
console.log("Inside fail function");
console.error(args.get_message() + '\n' + args.get_stackTrace());
});

});

siteUrl.fail(function (result) {
var error = result;
console.log(error);
});
}

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', intialize);


Output: Inside fail function

Unexpected response data from server.

null

Any thoughts on why this is not working?

EDIT (11/22/2016) :

I tried debugging the code where it reads the calendar events. I tried to hard code the URL and the calendar name and I am assuming that's where the problem is.

The code segment

var context = new SP.ClientContext('/gallery/Pages/events');
var list = context .get_web().get_lists().getByTitle('Calendar');


is not working while if I have a calendar in the same page the below code is working fine.

var context = SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('Calendar');


Is the URL syntax used is correct? I also tried using

var context = new SP.ClientContext(_spPageContextInfo.siteServerRelativeUrl + '/gallery/Pages/events');


But no luck so far.

• Is your siteurl correct ? request.siteUrl = site.SiteFullUrl; Are you trying to get data from other site collection or subsite ? – Siddharth Vaghasia Nov 22 '16 at 8:50
• @SiddharthVaghasia Yes! I am trying to get data from another subsite! – gilango Nov 22 '16 at 14:39
• @SiddharthVaghasia This is what the url parameter is '/gallery/Pages/events.aspx' I tried without '.aspx' as well. Do you think this is correct? – gilango Nov 22 '16 at 19:46
• @gilango yes the URL is wrong, you are supposed to use relative url or absolute url to the site. You are now using the URL to a page.aspx. Remove /Pages/Events – Anders Aune Nov 23 '16 at 13:11

## 1 Answer

Your JSOM seems okay (at first glance), so test the content

Trim this:

 request.query.set_viewXml('<View><Query><Where><DateRangesOverlap>' +
'<FieldRef Name=\"EventDate\">' +
'</FieldRef><FieldRef Name=\"EndDate\"></FieldRef>' +
'<FieldRef Name=\"RecurrenceID\"></FieldRef><Value Type=\"DateTime\">' +
'<Week/></Value></DateRangesOverlap>' +
'</Where></Query></View>');


to:

 request.query.set_viewXml('<View><Query></Query></View>');


then one-by-one extend the Query

• I tested the query itself which works fine. – gilango Nov 22 '16 at 20:16