Skip to content

Instantly share code, notes, and snippets.

@davehax
Last active January 24, 2017 08:45
Show Gist options
  • Select an option

  • Save davehax/037c92d5f25fd146a50d05ea4f28f5da to your computer and use it in GitHub Desktop.

Select an option

Save davehax/037c92d5f25fd146a50d05ea4f28f5da to your computer and use it in GitHub Desktop.
// Function to pad month and day value
function pad(number) {
var r = String(number);
if (r.length === 1) {
r = '0' + r;
}
return r;
}
var d = new Date();
// Construct ISO 8601 date strings by hand for the start and end of today
// these will be used in the caml query below
var todayStart = (d.getYear() + 1900) + '-' + pad((d.getMonth() + 1)) + '-' + pad(d.getDate()) + 'T00:00:00.000Z';
var todayEnd = (d.getYear() + 1900) + '-' + pad((d.getMonth() + 1)) + '-' + pad(d.getDate()) + 'T23:59:59.999Z';
// Get recurring events that occur today: http://georgetowndctechie.blogspot.com.au/2013/08/sharepoint-caml-query-to-get-todays.html -- it's lit
// CAML Query had to be changed slightly to include non-recurring events today
var caml = '<View>' +
'<Query>' +
'<OrderBy>' +
'<FieldRef Name="EventDate"/>' +
'</OrderBy>' +
'<Where>' +
'<Or>' +
'<And>' +
'<DateRangesOverlap>' +
'<FieldRef Name="EventDate"/>' +
'<FieldRef Name="EndDate"/>' +
'<FieldRef Name="RecurrenceID"/>' +
'<Value Type="DateTime" IncludeTimeValue="False">' +
'<Today/>' +
'</Value>' +
'</DateRangesOverlap>' +
'<Lt>' +
'<FieldRef Name="EventDate"/>' +
'<Value Type="DateTime">' +
'<Today/>' +
'</Value>' +
'</Lt>' +
'</And>' +
'<And>' +
'<Geq>' +
'<FieldRef Name="EventDate"/>' +
'<Value Type="DateTime">' + todayStart + '</Value>' +
'</Geq>' +
'<Leq>' +
'<FieldRef Name="EventDate"/>' +
'<Value Type="DateTime">' + todayEnd + '</Value>' +
'</Leq>' +
'</And>' +
'</Or>' +
'</Where>' +
'<QueryOptions>' +
'<IncludeMandatoryColumns>false</IncludeMandatoryColumns>' +
'<ViewAttributes Scope="Recursive"/>' +
'<RecurrencePatternXMLVersion>v3</RecurrencePatternXMLVersion>' +
'<ExpandRecurrence>true</ExpandRecurrence>' +
'<RecurrenceOrderBy>true</RecurrenceOrderBy>' +
'<ViewAttributes Scope="RecursiveAll"/>' +
'<CalendarDate>' +
'<Today/>' +
'</CalendarDate>' +
'</QueryOptions>' +
'</Query>' +
'</View>';
var endpoint = _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/GetByTitle('Example Calendar')/GetItems";
var requestData = JSON.stringify({
"query": {
"__metadata": {
"type": "SP.CamlQuery"
},
"ViewXml": caml
}
});
// Request form context. The reason for this being required is still a mystery!
// Without this code, the query against the Calendar will fail saying the form
// digest is invalid.
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/contextinfo",
method: "POST",
headers: {
"Accept": "application/json; odata=verbose"
}
})
.then(function (contextInfo) {
var request = $.ajax({
url: endpoint,
method: "POST",
data: requestData,
headers: {
"X-RequestDigest": contextInfo.d.GetContextWebInformation.FormDigestValue,
"Accept": "application/json; odata=verbose",
"Content-Type": "application/json; odata=verbose"
}
})
// Done and Fail handlers here...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment