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
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.