Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'd like to set up a CAML query in a Data View Web Part (DVWP) that works like this:

  • If a date is not specified on the Query String, then show me items created in the last 30 days
  • If a date is specified on the Query String, then show me items created from that date back 30 days

The first one is pretty easy:

<Where><Geq><FieldRef Name="Created"/><Value Type="DateTime"><Today OffsetDays="-30"/></Value></Geq></Where>

Offset Days seems to only be available with Today, though.

Any ideas?

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

What about approaching it from the other end of the solution? If users are clicking a link of some kind to get to this view (is it a data view web part?), you could use JavaScript (which can handle date offsets relatively easily) to append a query string with the start date and end date of the range you need, and use both of those parameters in your CAML query.

Or, if the date information is coming from a Date/Time column in a list, you could add a calculated column which can also handle date offsets relatively easily.

share|improve this answer
I think I'll probably end up doing it this way, but it would be nice to have one page do both things. –  Marc D Anderson Jun 8 '12 at 15:03
Can you use just one page and ALWAYS pass the start date and end date, even if it's for today -30? For the calculated column example, you could conditionally calculate today -30 if the original date column is empty, or calculate the date offset if it's not empty. –  Josh McCarty Jun 8 '12 at 15:13
It's the landing page for a site, so I can't calculate the default situation (Today -30). I'll end up with two pages. All fine, but less elegant. –  Marc D Anderson Jun 8 '12 at 15:17
This turned out to be the best way to go. I have two pages now: * The landing page shows the default view, which is today and up to 30 days back. * A second page takes a StartDate and EndDate on the Query String and filters for that date range. –  Marc D Anderson Jun 8 '12 at 19:35
add comment

You're probably wanting to do this client side right Marc?

The only thing that comes to my mind in that case is to grab the data in the query string and do your calculations in jQuery (assuming you're using your SPService library with this) and pass the hard dates into your CamlQuery as a beginning and end points and not do offsets.

share|improve this answer
add comment
DateTime endDate = Request.QueryString("StartDate") ?? DateTime.Now;//Edit as needed
DateTime startDate = endDate.AddDays(-30);

string query = string.Format(
      <FieldRef Name='Created'/>
      <Value Type='DateTime'>
      <FieldRef Name='Created'/>
      <Value Type='DateTime'>

Note that this doesn't just handle the second case, it handles both cases.

share|improve this answer
I'm trying to do this in a DVWP (added into the post above), so this syntax isn't available to me, but nice. –  Marc D Anderson Jun 8 '12 at 14:57
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.