Retrieving Expanded Calendar Events with REST vs. SOAP

There is very little now in 2013 for which you need SPservices [sic]

from /r/sharepoint

It wouldn’t surprise you to hear that my answer to this is “it depends”. SPServices is nice in that it works the same way across SharePoint versions from 2007-2013, which might be enough reason to use it. But there are still cases where the SOAP services do something that REST doesn’t [yet] provide.

The other day I ran across an example of this. I wanted to pull events from a SharePoint calendar from today forward and I wanted to expand all of the recurring events. Since I was working on SharePoint 2013, I immediately tried to make this work using a REST call.

The first hurdle I faced was that it wasn’t possible to filter on a data like StartDate in a REST query. I know that sounds crazy, but if it’s possible I couldn’t find any way to do it.

The other challenge was the expansion of recurring events. It turned out that this was only possible using SOAP. REST doesn’t know anything about SharePoint calendar events, especially recurrence or all-day events.

I ended going back to an old thread in the SPServices discussions started by Jim Bob Howard (@jbhoward) which gives the CAML to make this work. The code I ended up with is below. In this case, I was using KnockoutJS to populate a customized view of multiple SharePoint calendars – the “rollup” idea. We’ve the various calendar locations stored in a list, and when we get to this code we have an array of calendars to work with.

// Calendar rollup
var calendars = [];
var events = [];
var calendarPromises = [];

// For each calendar, go and get the events
for (var i = 0; i < calendars.length; i++) {

  var thisCalendar = calendars[i];
  var today = moment().format();

  calendarPromises[i] = $().SPServices.SPGetListItemsJson({
      listName : thisCalendar.title,
      webURL : thisCalendar.listUrl,
      CAMLViewFields : "<ViewFields>" +
			  "<FieldRef Name='ID' />" +
			  "<FieldRef Name='Title' />" +
			  "<FieldRef Name='EventDate' />" +
			  "<FieldRef Name='EndDate' />" +
			  "<FieldRef Name='Location' />" +
			  "<FieldRef Name='Description' />" +
			  "<FieldRef Name='Category' />" +
			  "<FieldRef Name='fRecurrence' />" +
			  "<FieldRef Name='RecurrenceData' />" +
			  "<FieldRef Name='fAllDayEvent' />" +
		"</ViewFields>",
      CAMLQuery : "<Query>" +
			  "<Where>" +
				  "<And>" +
					  "<DateRangesOverlap>" +
						  "<FieldRef Name='EventDate' />" +
						  "<FieldRef Name='EndDate' />" +
						  "<FieldRef Name='RecurrenceID' />" +
							  "<Value Type='DateTime'>" +
							  "<Year />" +
						  "</Value>" +
					  "</DateRangesOverlap>" +
					  "<Or>" +
						  "<Eq>" +
							  "<FieldRef Name='HomePageHide' />" +
							  "<Value Type='Bool'>False</Value>" +
						  "</Eq>" +
						  "<IsNull>" +
							  "<FieldRef Name='HomePageHide' />" +
						  "</IsNull>" +
					  "</Or>" +
				  "</And>" +
			  "</Where>" +
			  "<OrderBy>" +
				  "<FieldRef Name='EventDate' />" +
			  "</OrderBy>" +
		"</Query>",
      CAMLQueryOptions : "<QueryOptions>" +
			  "<CalendarDate>" + today + "</CalendarDate>" +
			  "<ExpandRecurrence>TRUE</ExpandRecurrence>" +
			  "<RecurrenceOrderBy>TRUE</RecurrenceOrderBy>" +
			  "<ViewAttributes Scope='RecursiveAll'/>" +
		"</QueryOptions>",
      mappingOverrides : {
        "ows_fAllDayEvent" : {
          "mappedName" : "fAllDayEvent",
          "objectType" : "Boolean"
        },
        "ows_fRecurrence" : {
          "mappedName" : "fRecurrence",
          "objectType" : "Boolean"
        }
      }
    });

}

$.when.apply($, calendarPromises).then(function () {

  var calendarEvents = this;

  // For each calendar, go and get the items and push them into an array
  for (var i = 0; i < calendars.length; i++) {

    $(calendarEvents[i].data).each(function () {
      events.push({
        calendar : calendars[i].title,
        id : this.ID,
        title : this.Title,
        location : this.Location != null ? this.Location : "",
        category : this.Category,
        eventDate : moment(this.EventDate),
        endDate : this.EndDate,
        fAllDayEvent : this.fAllDayEvent,
        fRecurrence : this.fRecurrence,
        RecurrenceData : this.RecurrenceData
      });
    });

  }

  events.sort(function (a, b) {
    return a.eventDate.isAfter(b.eventDate) ? 1 : (a.eventDate.isBefore(b.eventDate) ? -1 : 0);
  });

  ko.applyBindings(new calendarViewModel(), document.getElementById("calendar-rollup"));

});

The code is written to handle a variable number of calendars (we started with four). Because I’m putting the promise for each call to GetListItems in the calendarPromises array, I can use

$.when.apply($, calendarPromises)

to ensure that all of the promises have been fulfilled before processing the results.

I’m also using the fantastic MomentJS library to work with dates and times. Don’t ever try to do that manually again.

I’d love to hear that I don’t need to use SOAP for this anymore, but this seems to be a case where SOAP still wins out. Maybe there’s a place for SPServices in 2013, after all.

Similar Posts

8 Comments

  1. Hi Mark,

    I think you can achieve the same type of result with the REST API by executing a POST against the GetItems endpoint with a request body containing the CAML for the view.

    The following is an angular example:

    var listUrl = “https://.sharepoint.com/sites/testsite/_api/Web/lists/getbytitle(‘TestList’)”;
    var digest = “”;

    var viewXml = ”;
    return $http.post(listUrl + “/GetItems”, {
    query: {
    ViewXml: viewXml
    }
    }, {
    headers: {
    “X-RequestDigest”: digest
    }
    });

    All you would need to do is substitute for your CAML.

  2. Marc, you can filter on dates and/or a date range with REST as well, it’s just not overly fun.

    (*** if item.MyDate is retrieved via and sharepoint list, you don’t need to convert it to isostring***

    var _d = new Date(item.MyDate));
    var date = _d.toISOString();
    …. $filter=StartDate le datetime'” + date + “‘ and EndDate ge datetime'” + date + “‘

    1. Clint:

      I’ve seen multiple posts saying that is how to do it, but it must not work on premises. No permutation of that incantation would work for me, and I tried everything I could think of.

      M.

  3. In the column properties of the Event content type, EventDate and EndDate are set as Filterable=”FALSE”.
    Find it at “http://mySP2013/_api/Web/Lists(guid’00000000-0000-0000-0000-0000000000000′)/Fields”, find on page EndDate and look at SchemaXml value.

  4. Hi Marc D Anderson,

    As i am fresher , Kindly help me out.

    I Need Events like (EventDate=5/15/2018 – EndDate=5/19/2018, Title = MeetingEvents) which is with out Recurrence Event.

    On Daily Basis , it should show the Title until the EndDate?, but As your code shows only Recurrence Events.!

    Thanks in Advance.

  5. Hey Marc,

    I’m trying to use this code to pull back recurring events.

    I keep getting this error.

    “One or more field types are not installed properly. Go to the list settings page to delete these fields.”

    This is pretty much an OOTB calendar list in on-prem SP 2013. Have you run into that before or do you know why that might be happening?

    Thanks sir!

    -Sonny

      1. HA!!! I copied the above code and just added my calendar list info – I didn’t realize in the query there was a custom column in there specified – “HomePageHide”. DOH!

        Thanks Marc!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.