Skip to main content

Patrick Tisseghem's Blog [MVP SharePoint]

Go Search
U2U Blog Center
U2U website
  

The Happy SharePoint Traveller

U2U Blog Center > Patrick Tisseghem's Blog [MVP SharePoint] > Posts > Using Today in a CAML Query
Using Today in a CAML Query

Going through my comments here on the blog, I notice that there are quite a number of comments on CAML-related postings. Here is an interesting one where I decided to call the help of our CAML girl to have a look at them. It will of course cost me a couple of beers, but heck, I do have an answer now smile_regular

 

Would it be possible to introduce something like Today - 5 (days ?)

Answer:

If you want to work with the date of today, you could use the syntax <Today />  and if you want to do simple calculations f.e. adding or subtracting a certain number of days of today’s date you could make use of the syntax <Today OffsetDays=”5” />. In a content query web part you could integrate a query like the following:

<WHERE>
<GE>
<FieldRef Name="StartDate"/>
<Value Type="DateTime"><Today OffsetDays=5 /></Value>
</GE>
</WHERE>

If you are writing code, you could build and execute your CAML query  as follows:

SPSite site = new SPSite("http://wss.u2ucourse.com");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["Course Calendar"];
string querystring = "<WHERE><GE><FieldRef Name=\"StartDate\"/>"
   
+ "<Value Type=\"DateTime\"><Today OffsetDays=\"5\" /></Value></GE></WHERE>";
SPQuery query = new SPQuery();
query.Query = querystring;
DataTable table = list.GetItems(query).GetDataTable();

In case of more complex calculations on dates, you could make use of SPUtility.CreateISO8601DateTimeFromSystemDateTime (as specified in previous post) in combination with the different calculation methods on the DateTime data type.  For completeness, I integrate a code example based on the previous example:

SPSite site = new SPSite("http://wss.u2ucourse.com");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["Course Calendar"];
string datestring =
  Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime
  (DateTime.Today.AddDays(5));

string querystring = "<WHERE><GE><FieldRef Name=\"StartDate\"/>"

  + "<Value Type=\"DateTime\">" + datestring + "</Value></GE></WHERE>";
SPQuery query = new SPQuery();
query.Query = querystring;
DataTable table = list.GetItems(query).GetDataTable();

As a side note, build such queries will be possible with the new version of the CAML Query Builder feature that will be released soon.

Comments

Re: Using Today in a CAML Query

Wow. Why is this not documented?
I remember writing a simple web part to display all events from a list occuring in the next 7 days, and finding myself forced to write a C# web-part rather than a DVWP because I couldn't define a sliding time frame in CAML. This is a great feature - thanks for the tip.
at 28/11/2007 9:24

Re: Using Today in a CAML Query

This is definitely something to know i was on a project when i needed such a solution i had to dig it out myself.
Blogged it too at:
http://geekswithblogs.net/naijacoder/archive/2007/11/20/117017.aspx

at 22/01/2008 6:05

What about using Now?

Using the <Now/> tag works fine for me in "normal" caml-queries, e.g. in views. But it doesn't work if I use it in a query in the ContentQueryWebPart.
I know the CQWP SPSiteDataQuery instead of the SPQuery to be able to query across sites. But how come it doesn't support the <Now/> tag? Or am I doing something wrong?
This works fine:
<When><Lt><FieldRef Name="StartDate"/><Value Type="DateTime"><Today/></Value></Lt></When>
But this doesn't:
<When><Lt><FieldRef Name="StartDate"/><Value Type="DateTime"><Now/></Value></Lt></When>
at 27/02/2008 15:26

CalendarDate for DateRangesOverlap

In the case of the DateRangesOverlap element you can also use the CalendarDate property of the SPQuery class to specify the value for Today. For example, if you set  CalendarDate to DateTime.Today.AddDays(-5) then Today will represent the day before five days intstead of the 'real' today. This trick seems not to work with other elements like Eq or Gt.
at 3/03/2008 23:41

From saro

So for  here i have given my feedback .
you will give a full code for calender list.
like the gridview webparts get the event from calender list which is exactly match a current date.
at 17/07/2008 11:16

Re: What about using Now?

You need to include the "IncludeTimeValue='TRUE'" variable:

<When><Lt><FieldRef Name="StartDate"/><Value Type="DateTime" IncludeTimeValue='TRUE'><Today/></Value></Lt></When>
at 18/11/2008 4:43
Captcha

Enter the code shown above: *

(Note: If you cannot read the numbers in the above
image, reload the page to generate a new one.)