SharePoint 2013: Recursive Calendar Event

The code in this post will show how to create recursive calendar events. The data about the Recurrence is stored in a field named "RecurrenceData". It is xml based data. One of the easy ways to determine what the value of "RecurrenceData" should be, is to create a recurrence item in the list from UI, get that item from code and find the data. Example:
SPList cal = web.Lists["Cal"];
SPListItem item = cal.GetItemById(1);
string data = item["RecurrenceData"].ToString();
Here is sample recurrence xml data
<recurrence><rule><firstDayOfWeek>su</firstDayOfWeek><repeat><weekly tu="TRUE" weekFrequency="1" /></repeat><repeatInstances>2</repeatInstances></rule></recurrence>
This is how it shows in UI. It is a weekly recurrence:

Weekly Recurrence

There are few other fields which need to be updated when creating a Recurrence event apart from "RecurrenceData". "EventType" which needs to be set to 1,"UID" which takes a GUID and "Recurrence" which is set to true. Here is a sample code which creates a Recurrence event.
static void Main(string[] args)
{
    using (SPSite site = new SPSite("http://aissp2013/sites/Team1/"))
    {
        SPWeb rootWeb = site.RootWeb;
        SPList cal = rootWeb.Lists["AIS Calendar"];
        SPListItem newItem = cal.AddItem();
        newItem["Title"] = "IDC Townhall";
        newItem["Location"] = "WAR Room 13";
        newItem["EventDate"] = new DateTime(2015,2,10, 10,0,0);
        newItem["EndDate"] = new DateTime(2015, 2, 10, 11, 0, 0);
        newItem["Recurrence"] = true;
        newItem["RecurrenceData"] = "<recurrence><rule><firstDayOfWeek>su</firstDayOfWeek><repeat><weekly tu='TRUE'" +
        " weekFrequency='1' /></repeat><repeatInstances>2</repeatInstances></rule></recurrence>";
        newItem["EventType"] = 1;
        newItem["UID"] = System.Guid.NewGuid();
        newItem.Update();
        cal.Update();
    }
}
Here is the Recurrence event in the UI created as a result of above code:

Recurrence Event


profile for Nadeem Yousuf at SharePoint Stack Exchange, Q&A for SharePoint enthusiasts

+