DCSIMG
Getting current events from a Calendar list - itaysk

Getting current events from a Calendar list

Posted Friday, July 25, 2008 1:39 PM by Itay Shakury

You have a calendar list. You want to query it for events that happen right now. Not today, not this week, right this moment. Yes, including reoccurring events. I really thought it would be easier, or at least hoped to find a working example somewhere...

Anyway, here is the CAML that I ended up with:

<Where> 
    <And>  
        <DateRangesOverlap>  
            <FieldRef Name="EventDate" />  
            <FieldRef Name="EndDate" />  
            <FieldRef Name="RecurrenceID" />  
            <Value Type="DateTime" IncludeTimeValue="TRUE">  
                <Today />  
            </Value>  
        </DateRangesOverlap>  
        <And>  
            <Geq>  
                <FieldRef Name="EndDate" />  
                <Value Type="DateTime" IncludeTimeValue="TRUE"> 
                    <Today /> 
                </Value>  
            </Geq>  
            <Leq>  
                <FieldRef Name="EventDate" />  
                <Value Type="DateTime" IncludeTimeValue="TRUE"> 
                    <Today /> 
                </Value>  
            </Leq>  
        </And>  
    </And>  
</Where>

The first part gets all the events for today, including reoccurring events, and the second one checks for the time. Put the both inside <And> section, and you get all the events for now.
DatesOverlap is used to query for reoccurring events, it needs some configuring of the SPQuery object to work.

Here is how it all fits togeather in code:

SPList list = SPWeb.Lists["Calendar"];
SPQuery q = new SPQuery(); 
q.CalendarDate = DateTime.Now; 
q.ExpandRecurrence = true; 
q.Query = "<Where>...</Where>"; //Full CAML omitted for clarity, just paste the code from above
SPListItemCollection items = list.GetItems(q);

Hope this saves you the time I spent.

תגים:,

Comments

# re: Getting current events from a Calendar list

Thursday, August 21, 2008 11:54 AM by Anuu

hi

can you help me for creating Event calendar webpart in moss 2007 for calendar list.

Can you provide me any kind of code ???/

# re: Getting current events from a Calendar list

Tuesday, August 26, 2008 7:48 AM by Itay Shakury

# re: Getting current events from a Calendar list

Thursday, December 04, 2008 2:21 PM by shereen

thank you so much for this info! i was trying to figure out how to query on a datetime field and some articles had me using dateTime.now which wasn't working at all. this was very helpful.

shereen

# re: Getting current events from a Calendar list

Friday, December 19, 2008 7:41 PM by tecrms

is there a way to use the CAML info along with a CQWP to produce the reoccurring events results?

# re: Getting current events from a Calendar list

Thursday, August 26, 2010 11:38 AM by Nicholas

Hello,

In SharePoint Designer 2010, if I have an XsltListViewWebPart of a Calendar list on a page, I can view recurring events by using the <DateRangesOverlap> tags, however if I combine this with other conditions in an <And> clause as you have above, the reoccuring events are no longer included. For example:

<Where>

 <DateRangesOverlap>

  <FieldRef Name="EventDate"/>

  <FieldRef Name="EndDate"/>

  <FieldRef Name="RecurrenceID"/>

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

 </DateRangesOverlap>

</Where>

returns all events for the week, including any instances of recurring events that occur during the week. However, if i modify the query to filter only events within the next 5 days using:

<Where>

<And>

 <DateRangesOverlap>

  <FieldRef Name="EventDate"/>

  <FieldRef Name="EndDate"/>

  <FieldRef Name="RecurrenceID"/>

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

 </DateRangesOverlap>

 <And>

  <Geq><FieldRef Name="EventDate" /><Value Type="DateTime"><Today /></Value></Geq>

  <Leq><FieldRef Name="EventDate" /><Value Type="DateTime"><Today OffsetDays="5" /></Value></Leq>

 </And>

</And>

</Where>

then any recurring events that should occur during the week are not displayed?

# re: Getting current events from a Calendar list

Monday, October 10, 2011 2:03 PM by Essentie

Thanks man great post!

# re: Getting current events from a Calendar list

Tuesday, November 15, 2011 3:46 PM by Essentie

I've just been testing your solution and found that when i create a reccuring event that is not happenning right now (for example, from 9 to 11 every day and now it's 12 o'clock), it comes out from your code that it's the current event.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: