Thursday 24 March 2011

Date filter in XSLT views for [Current Date] Plus or Minus a certain number of days

Creating a filter on a date field where you want to filter on the [Today] plus or minus a set number of days (e.g. in the past 14 days) in the web front end is pretty easy.  The problem comes when trying to achieve that in designer for an XSLT view.  There you can apparently only filter on [Current Date].  A work aroudn for this is to create the view initially in the web front end then load in Designer and convert to XSLT.

If you then view the filter in Designer it shows only [Current Date] but still works with the Plus or Minus.  The downfall is that if you then subsequently edit the filter (e.g. to add other clauses) the date parts start filtering just on [Current Date], they lose the Plus or Minus.  To avoid this before making any changes select the split view then select the Webpart in the 'Design' pane.  In the Code pane the related code will be highlighted.  Look for a line near the start which begins

<code>&lt;SharePoint:SPDataSource runat="server" DataSourceMode="List" SelectCommand=</code>

  Read this line and look for a section that reads something like:

<code>Type=&quot;DateTime&quot;&gt;&lt;Today OffsetDays=&quot;+14&quot;/&gt;&lt;/Value&gt</code>

copy the section following the word 'Today' up to the next slash (/) and paste into another program (e.g. Notepad).  In the above example you would copy out :

<code>OffsetDays=&quot;+14&quot;</code>

If there are multiple date filters you will need to do this for each filter.  Then edit the filter in the GUI (add clauses, edit non-date clauses &c).  Once this has been done go back to the code pane, you will see that the OffsetDays has been removed.  In the above example it would now be:

<code>Type=&quot;DateTime&quot;&gt;&lt;Today /&gt;&lt;/Value&gt</code>

Copy and paste back in the text and the filter should now work correctly.

1 comment: