Tuesday, November 4, 2008

Filter between two dates (date range) with the Data view Webpart

To filter a date range in sharepoint:

1. What you need on a page:
a. 2 Date Filter webparts
b. 1 list to be filtered

2. Open the page w/ SPD, convert the list to be filtered to DataView (Convert to DataView will give you a lot more columns to choose in the DataView’s Filter critiera, as oppose to going to Insert->Sharepoint Controls->DataView. However, you will still not see a ‘[StartDate]’ and ‘[EndDate]’ when you go to Filter on the DataView's 'Common Date View Tasks'.

3. Now it's time to create the 'StartDate' and 'EndDate' parameters:
Under the DataView's 'Common Date View Tasks', choose Filter, click on the 'Click here to add a phase', scroll down and click on 'Create a new parameter'




4. Now we are ready to configure the filter for the Date View:



5. In the code view, you should see the newly added parameters:
<ParameterBinding Name="StartDate" Location="QueryString(StartDate)" DefaultValue=""/>
<ParameterBinding Name="EndDate" Location="QueryString(EndDate)" DefaultValue=""/>


6. Now we need to setup the connections from the DataView so it knows where to get the data for the parameters. Click on the little arrow for ‘Common Dataview tasks’, go to webpart connections (Note: makes sure you choose the ‘Get Parameters From’)



When done going through the Web part Connections Wizard, you should see something like this:



7. Almost done. Now go to the browser and edit page. Click on the date filters to connect the webparts. The Data needs to know where to get the filter values. Even though the connections were setup from the DataView, you still have to setup the filter fields from the Date Filters. Note the consumer field name is the parameter we setup earlier. Do the same for each date filter, one for the 'StartDate' parameter, one for the 'EndDate' parameter.



8. Done. Final result could be something like this: