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.