Retrieve the events from a SharePoint calendar using REST API
The events for a current month could be retrieved via the following CAML query:
<Where>
<DateRangesOverlap>
<FieldRef Name='EventDate' />
<FieldRef Name='EndDate' />
<Value Type='DateTime'>
<Month />
</Value>
</DateRangesOverlap>
</Where>
For that matter GetItems
method could be utilized as demonstrated below:
var query = `
<Where>
<DateRangesOverlap>
<FieldRef Name='EventDate' />
<FieldRef Name='EndDate' />
<Value Type='DateTime'>
<Month />
</Value>
</DateRangesOverlap>
</Where>`
getListItems(_spPageContextInfo.webAbsoluteUrl,'TeamCalendar',query)
.done(function(data){
var items = data.d.results;
for(var i = 0; i < items.length;i++) {
console.log(items[i].Title);
}
})
.fail(function(error){
console.log(JSON.stringify(error));
});
where
function getListItems(webUrl,listTitle, queryText)
{
var viewXml = '<View><Query>' + queryText + '</Query></View>';
var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems";
var queryPayload = {
'query' : {
'__metadata': { 'type': 'SP.CamlQuery' },
'ViewXml' : viewXml
}
};
return $.ajax({
url: url,
method: "POST",
data: JSON.stringify(queryPayload),
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"Accept": "application/json; odata=verbose",
"content-type": "application/json; odata=verbose"
}
});
}
But there is one limitation with this approach, neither REST nor CSOM/JSOM APIs do not support expanding for recurring events (it means only a single event item will be returned for a recurring event). Refer those requests for a more details:
- Provide CSOM and REST API for recurring calendar events
- Add support for recurring events in the REST API
For that scenario legacy SharePoint Web Services comes to the rescue, in particular Lists Web Service.
The following example demonstrates how to retrieve events for a current month and expand recurring events (SPServices library is utilized here):
$().SPServices({
operation: "GetListItems",
async: false,
listName: "TeamCal",
CAMLViewFields: "<ViewFields>" +
"<FieldRef Name='Title' />" +
"<FieldRef Name='EventDate' />" +
"<FieldRef Name='EndDate' />" +
"<FieldRef Name='Location' />" +
"<FieldRef Name='Description' />" +
"<FieldRef Name='fRecurrence' />" +
"<FieldRef Name='RecurrenceData' />" +
"<FieldRef Name='fAllDayEvent' />" +
"</ViewFields>",
CAMLQuery: "<Query>" +
"<Where>" +
"<DateRangesOverlap>" +
"<FieldRef Name='EventDate' />" +
"<FieldRef Name='EndDate' />" +
"<FieldRef Name='RecurrenceID' />" +
"<Value Type='DateTime'>" +
"<Month />" +
"</Value>" +
"</DateRangesOverlap>" +
"</Where>" +
"<OrderBy>" +
"<FieldRef Name='EventDate' />" +
"</OrderBy>" +
"</Query>",
CAMLQueryOptions: "<QueryOptions>" +
"<RecurrencePatternXMLVersion>v3</RecurrencePatternXMLVersion>" +
"<ExpandRecurrence>TRUE</ExpandRecurrence>" +
"</QueryOptions>",
completefunc: function (xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function() {
var $node = $(this);
var eventTitle = $node.attr("ows_Title");
console.log(eventTitle);
});
}
})
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
I have the rest endpoint for accessing the sharepoint calendar from my internal network at work. It looks like this: https://teamsites.{COMPANY}.com/sites/{SITE_URI}/_api/Web/Lists/getbytitle({TITLE-OF-CALENDAR})/items
When I do an authorized GET request to the above url, it gives me a list of 4 events from the calendar, but the dates are kind of random. What is the endpoint I need to specify in order to retrieve all of the events for the current month?
Also, I'm guessing that the reason I'm only getting 4 events per request is because of some pagination thing that I'm not handling... if you know how to retrieve all the events in one request, that would be a bonus.
Thanks in advance!