Introduction


Environment: SharePoint 2013 Enterprise
Requirement: Programmatically get the recurrence data
The following example shows how to get the recurrence data programmatically from a calendar list.
In this example the recurrence event is set to occur weekly every Wednesday with no end date.



Following the view of the calendar list.



CAML Query

Following is the CAML query that is used to retrieve the recurrence data. 

Query =
@"<Where>
<DateRangesOverlap>
      <FieldRef Name='EventDate' />
      <FieldRef Name='EndDate' />
       <FieldRef Name='RecurrenceID' />
<Value Type='DateTime'><Month /></Value>
</DateRangesOverlap>
</Where>
<OrderBy>
<FieldRef Name='EventDate' Ascending='True' />
</OrderBy>",
ViewFields = "<FieldRef Name='Title' />
<FieldRef Name='EventDate' />
<FieldRef Name='EndDate' />
<FieldRef Name='fRecurrence' />
<FieldRef Name='RecurrenceData' />
<FieldRef Name='Location' />",
ExpandRecurrence = true,
CalendarDate = DateTime.Now

ExpandRecurrence property is set to true to specify whether to expand recurrent events in the calendar view.
CalendarDate property is sets the start date from the when the events are retrieved

Source Code



Following is the full source code to get the data
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spsite = new SPSite("http://siteURL/"))
{
using (SPWeb web = spsite.OpenWeb())
{
SPList list = web.Lists["MyCalendar"];
SPQuery query = new SPQuery();
var eventsQuery = new SPQuery
           {          
Query =
@"<Where>
<DateRangesOverlap>
      <FieldRef Name='EventDate' />
      <FieldRef Name='EndDate' />
       <FieldRef Name='RecurrenceID' />
<Value Type='DateTime'><Month /></Value>
</DateRangesOverlap>
</Where>
<OrderBy>
<FieldRef Name='EventDate' Ascending='True' />
</OrderBy>",
ViewFields = "<FieldRef Name='Title' />
<FieldRef Name='EventDate' />
<FieldRef Name='EndDate' />
<FieldRef Name='fRecurrence' />
<FieldRef Name='RecurrenceData' />
<FieldRef Name='Location' />",
ExpandRecurrence = true,
CalendarDate = DateTime.Now
};
   
SPListItemCollection listItems;
listItems = list.GetItems(eventsQuery);
foreach (SPListItem item in listItems)
{
                //Returns the next recurrence date
Console.WriteLine("Event: " + item["EventDate"].ToString()); 
//Returns recurrence data
Console.WriteLine("Recurrence Data: " + item["RecurrenceData"]);
  
            }
}
           }
       });

Recurrence Data

The recurrence data can be retrieved by Day, week, month or year. Specify the value type as per the requirement. CalendarDate property value is considered as start date.

 Following are the Value Types

//Today – Returns events for the current day
// Week – Retrieves events across the week
// Month – Gets all events for the current month
// Year – Retrieves events that occur in the year


Following is the output of the above code when the value is passed as Month which gets all the events for the month.

<Value Type='DateTime'><Month /></Value>

Event: 9/9/2015 12:00:00 AM
Recurrence Data: <recurrence><rule><firstDayOfWeek>su</firstDayOfWeek><repeat><weekly we="TRUE" weekFrequency="1" />
</repeat><repeatForever>FALSE</repeatForever></rule></recurrence>
Event: 9/16/2015 12:00:00 AM
Recurrence Data: <recurrence><rule><firstDayOfWeek>su</firstDayOfWeek><repeat><weekly we="TRUE" weekFrequency="1" />
</repeat><repeatForever>FALSE</repeatForever></rule></recurrence>
Event: 9/23/2015 12:00:00 AM
Recurrence Data: <recurrence><rule><firstDayOfWeek>su</firstDayOfWeek><repeat><weekly we="TRUE" weekFrequency="1" />
</repeat><repeatForever>FALSE</repeatForever></rule></recurrence>
Event: 9/30/2015 12:00:00 AM
Recurrence Data: <recurrence><rule><firstDayOfWeek>su</firstDayOfWeek><repeat><weekly we="TRUE" weekFrequency="1" />
</repeat><repeatForever>FALSE</repeatForever></rule></recurrence>

Following is the output of the above code when the value is passed as Week which gets all the events for the week.

<Value Type='DateTime'><Week /></Value>

Event: 9/9/2015 12:00:00 AM                                                     
Recurrence Data: <recurrence><rule><firstDayOfWeek>su</firstDayOfWeek><repeat><weekly we="TRUE" weekFrequency="1" />
</repeat><repeatForever>FALSE</repeatForever></rule></recurrence>