Sunday, October 30, 2011

SharePoint: CAML get all calendar items from including recurring items

Situation:
I want to get all calendar items from today onwards including any recurring event items.

Problem:
CAML and SharePoint can be a pain.

Solution:
SPQuery query = new SPQuery();
string queryString =
@"
    <Where>
     <DateRangesOverlap>
      <FieldRef Name=""EventDate""/>
      <FieldRef Name=""EndDate""/>
      <FieldRef Name=""RecurrenceID""/>
      <Value IncludeTimeValue=""TRUE"" Type=""DateTime"">
       <Now/>
      </Value>
     </DateRangesOverlap>
    </Where>
    <OrderBy>          
 <FieldRef Name=""EventDate"" Ascending=""TRUE""/>      
    </OrderBy>
";
query.Query = queryString;
query.ExpandRecurrence = true;

Note:
You should add a query.RowLimit so you don't get too many items back. But that's up to you.

3 comments:

  1. Great I have been searching for days for this
    Thank you

    ReplyDelete
  2. Good Day I am new to sharepoint and coding how would I use this to populate a java calendar ?

    ReplyDelete