How to get recurrence events? For Today and for this Month?

SharePoint provides powerful calendar object model support for developers to leverage on. As it suffers from the lack of kickass documentation, one gets used to it with practice.

Recurring events are such. The major task with such events is to query them correctly so as to avoid in code checks which can burden the web part's performance.So use SpQuery properties correctly.

For example, to get all the recurrence events for this month:

//Get the first day of the Month
                DateTime caldate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                SPQuery query = new SPQuery();
                //Check the  tag, that's important to retrive a month of events including the recurrence one
                query.Query = "";
                query.ExpandRecurrence = true;
                //feed the month's first day to query
                query.CalendarDate = caldate;
                //fire the query to get list of items
                SPListItemCollection items = lst.GetItems(qry);

As a second example, to get all the recurrence events for today
//Get the date for today
                DateTime caldate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                SPQuery query = new SPQuery();
                //Check the  tag, that's important to retrive all events including the recurrence one for today
                query.Query = "";
                query.ExpandRecurrence = true;
                //feed the month's first day to query
                query.CalendarDate = caldate;
                //fire the query to get list of items
                SPListItemCollection items = lst.GetItems(qry);

So if the dates are correct and CAML are well formed, we get our results correctly.

No comments:

Post a Comment