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)');
                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'];


                function (sender, args) {
                    // Failure
        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>' + 
            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();
                            Title: oListItem.get_item('Title'),
                            EventDate: oListItem.get_item('EventDate'),
                            Description: oListItem.get_item('Description'),
                            EndDate: oListItem.get_item('EndDate')

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


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

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

Output: Inside fail function

Unexpected response data from server.


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 ? Nov 22, 2016 at 8:50
  • @SiddharthVaghasia Yes! I am trying to get data from another subsite!
    – Gowthaman
    Nov 22, 2016 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?
    – Gowthaman
    Nov 22, 2016 at 19:46
  • 1
    @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 Nov 23, 2016 at 13:11

1 Answer 1


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>' + 



then one-by-one extend the Query

  • I tested the query itself which works fine.
    – Gowthaman
    Nov 22, 2016 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.