2

So far I'm able to get the next x events (without recurring events). Code:

    private SPListItemCollection GetNextEvents(SPList eventCalendar)
    {
        DateTime currentDateTime = DateTime.Now;
        string currentDateTimeString = SPUtility.CreateISO8601DateTimeFromSystemDateTime(currentDateTime);
        SPQuery query = new SPQuery();
        query.RowLimit = 3;
        query.ExpandRecurrence = true;            
        query.Query = "<Where>" +                    
                          "<And>" +
                            "<Geq>" +
                                "<FieldRef Name='EndDate' />" +
                                "<Value Type='DateTime' IncludeTimeValue='True'>" +
                                    currentDateTimeString +
                                "</Value>" +
                            "</Geq>" +
                            "<Geq>" +
                                "<FieldRef Name='EventDate' />" +
                                "<Value Type='DateTime' IncludeTimeValue='True'>" +
                                    currentDateTimeString +
                                "</Value>" +
                            "</Geq>" +
                          "</And>" +
                        "</Where>" +
                        "<OrderBy>" +
                            "<FieldRef Name='EventDate' />" +
                        "</OrderBy>";            

        SPListItemCollection itemColl = eventCalendar.GetItems(query);

        return itemColl;
    }

Setting query.ExpandRecurrence = true didn't help. I also tried the solution suggested here. Somehow I can't get it working with recurring events. For the following example I'd like to get the 2nd and 3rd "OK Repeating Event" and the "OK1" event since it starts earlier than "OK2" CalendarImage

1 Answer 1

1

Apart from setting ExpandRecurrence property of SPQuery to true, you also need to specify the DateRangesOverlap in the query to get the recurrence events. Example:

SPList list = web.Lists["Company Calendar"];                                       
SPQuery oQuery = new SPQuery();
oQuery.ExpandRecurrence = true;                     
oQuery.Query = "<Where><DateRangesOverlap><FieldRef Name='EventDate' />" +
"<FieldRef Name='EndDate' /><FieldRef Name='RecurrenceID' />" +
"<Value Type='DateTime'><Now /></Value>" +
"</DateRangesOverlap></Where>";                     
oQuery.CalendarDate = DateTime.Now;                    
SPListItemCollection items = list.GetItems(oQuery);                    
foreach(SPListItem item in items)
{
    Console.WriteLine("Title: {0}, StartDate: {1}", item.Title, item["EventDate"]);
} 

See Recurring Calendar Events for more information.

5
  • Problem is that DateRangedOverlap also returns events that started in the past what I do not want (for example the NOK1 event from the picture). Is there a way to combine DateRangesOverlap with the query from above which returns items that start and end in the future?
    – Razze
    Commented Jul 13, 2015 at 7:24
  • You can use And/Or operators to create such query. Commented Jul 13, 2015 at 12:43
  • It seems I don't see the woods for the trees. From my understanding the And-Operator doesn't make sense because the recurring events come from the DateRangesOverlap part and won't get returned with the query from my original post. The Or-Operator wouldn't work because the DateRangesOverlap query returns items that begun in the past and end today, which I don't want to get returned.
    – Razze
    Commented Jul 14, 2015 at 10:59
  • Using Now in DateRangesOverlap query will give you events from today and the events which are within 2 years in future. Commented Jul 14, 2015 at 11:36
  • I am using Now as in your example but it still returns items that started in the past and end today (only the day seems to matter for the EndDate, not the time. Using IncludeTimeValue='True' for the Value element doesn't help)
    – Razze
    Commented Jul 14, 2015 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.