Friday, May 25, 2012

SharePoint 2010 LVWP Dynamic Date Filtering

Situation: You used SharePoint Designer 2010 to insert what you *thought* was a data view onto a web part page and it is actually a List View Web Part (LVWP) and you want to filter the data related to [Today]. When you click the 'Filter' button in the top-left of SPD2010, it will only let you filter to '[CurrentDate]' and doesn't give you the option to say something like '[Today]-10'.

Resolution: There are two ways to alter the core query here:
1 - No looking at the dreaded 'code' or 'CAML': Save the page in SharePoint Designer, open the page directly in your browser, modify the web part by clicking its little dropdown and clicking 'Edit Web Part' and clicking 'Edit Current View' underneath the Current View dropdown (and then fix like you would any other view...sometimes this may actually give you an error if your LVWP was being evil like mine).
2 - Must look at 'code' and 'CAML': When you look at the evil 'code' behind your data view, you will notice a section that looks like this near the top of your LVWP:
<View name="{123jk123-j123-12j1-j1k2l;3l123j}" Mobile View="TRUE" Type="HTML"....
          <OrderBy />
          <GroupBy />

If you have already filtered to '[CurrentDate]' then it will have put a section inside the <Query> that looks similar to this:
          <FieldRef Name="YourDateField"/>
          <Value Type="DateTime">

What to fix: that lovely <Today/> piece. If you want to say something like 'Show me everything that is greater than (aka after) the last 10 days' then you would need to:
1) Add an 'OffsetDays' attribute to the Today so that it looks like this: <Today OffsetDays="-10"/>
2) Make sure that where I have "Eq", you put "Gt" (short for Greater Than) -- You could do the reverse by setting it to Lt for "Less Than" and making the OffsetDays a positive number to make it something like [Today]+7.


  1. This comment has been removed by the author.

  2. The OffsetDays attribute is on the < Today > element and not on the < Value > element.