0

I've been trying to get all events that are today as a test, I haven't been able to do that either. So far I've set an event for today (an all day event so times don't matter) and executed this query:

<Where>
  <Contains>
    <FieldRef Name='StartDate' />
      <Value Type='Date'>
        <Now />
      </Value>
    </FieldRef>
  </Contains>
</Where>

but still no luck.

4
  • Did you get this working? Commented Jan 8, 2013 at 2:27
  • Yeah, here's my query: <Where><Eq><FieldRef Name='EventDate' /> <Value Type='DateTime'><Today /> </Value></Eq></Where>
    – cbrad
    Commented Jan 8, 2013 at 6:28
  • Ok, cool, and that works with recurring events? Or is recurring events not a case you are concerned with? Commented Jan 8, 2013 at 18:04
  • It doesn't matter, as long as there is an event that day, it will fire. It checks every 24 hours.
    – cbrad
    Commented Jan 10, 2013 at 1:32

2 Answers 2

1

Start Date in the calendars has an internal name of EventDate. Try that instead, otherwise the CAML looks good to me.

1
1

Because recurring event's exist this has been difficult for me in the past. I've usually had to limit it to a particular month and then eliminate items that didn't meet my criteria in code after the query returns. I know it sucks but it's the only way I've been able to get all of the events.

Below is the spquery code I've used before. The key is the expandrecurrance and the calendardate properties.

SPQuery eventQuery = new SPQuery();
eventQuery.ExpandRecurrence = true;
eventQuery.CalendarDate = new DateTime("12/20/2012");

The SPUtility.CreateISO8601DateTimeFromSystemDateTime function is handy when trying to create your caml. Below is some caml that worked for me, the key is the daterangesoverlap section which helps bring in all of the recurring events for the month. Also, since i'm usually outputting a monthly calendar, I bring in events from the previous and next months to make sure they show correctly when the monthly calendar shows events in the same week for a previous or next month. You can probably adjust the dates to be more exact since you are talking about a single day.

<Where>
    <Or>
        <DateRangesOverlap>
            <FieldRef Name="EventDate" />
            <FieldRef Name="EndDate" />
            <FieldRef Name="RecurrenceID" />
            <Value Type="DateTime">
                <Month>12</Month>
                <Year>2012</Year>
            </Value>
        </DateRangesOverlap>
        <Or>
            <And>
                <Geq>
                    <FieldRef Name='EventDate' />
                    <Value Type='DateTime'>2012-11-20T00:00:00Z</Value>
                </Geq>
                <Leq>
                    <FieldRef Name='EventDate' />
                    <Value Type='DateTime'>2013-01-20T00:00:00Z</Value>
                </Leq>
            </And>
            <And>
                <Geq>
                    <FieldRef Name='EndDate' />
                    <Value Type='DateTime'>2012-11-20T00:00:00Z</Value>
                </Geq>
                <Leq>
                    <FieldRef Name='EndDate' />
                    <Value Type='DateTime'>2013-01-20T00:00:00Z</Value>
                </Leq>
            </And>
        </Or>
    </Or>
</Where>
5
  • Going to give this a try. Thanks for your detailed input!
    – cbrad
    Commented Dec 21, 2012 at 2:32
  • So this will only work for year 2012 - 2013 right? Can I format the date returned from the DateTime method so that I can use it in the query?
    – cbrad
    Commented Dec 21, 2012 at 2:37
  • Yea, I was just showing you the final result of my query. I created the query in code and used the appropriate dates. Commented Dec 21, 2012 at 3:27
  • Ok cool. So the second event date in the query is the date later than the first correct? So in my query I would have the date today as the first date in the query, and then add 1 day to today as my second date in the query?
    – cbrad
    Commented Dec 21, 2012 at 16:32
  • Well, my caml above is something like this in pseudo code: Return event if (a recurring event overlaps for the month 12/2012) OR ((EventDate > 11/20/2012 and EventDate < 01/20/2013) OR (EndDate > 11/20/2012 and EndDate < 01/20/2013)) Since you are only doing it for one day, you may not need the range of dates like I have. But you will have to play around with it to be sure. Commented Dec 21, 2012 at 16:38

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.