Saturday, April 16, 2011

Filtered Dataview Web-part using SharePoint Designer

Filtered Dataview web part - Our requirement was to show the related project tasks/issue in the dispform of the project details of the project list, something similar to the Out of the box Project management site template.

Here we are displaying the project tasks under the Project list, filtered by the lookup column "Project" in the Tasks list.

The filtering is done using XSLT using the SelectCommand property.

Steps:
Add the Data View underneath the Projects list form webpart in the dispform.aspx of project list and get the data you want to insert the view of.  Once you have the data view web part there showing all the data in the list (non filtered), switch to Code view.
In the inserted Dataview, just after the tag add the following (If there is already a DataSources tag, then insert only the ....tag.... )


 This is where the data view fetches its data. The "SelectCommand" property of that tag is what applies the filter we want. 
Explanation for the above datasource query is below:
It compares the ID value of the "Project" field in a project task with the current ID of the query string (i.e. currently displayed object).
What makes it work is Type='Integer' and LookupId='True'. This turns the Project value in the Project Tasks list into the equivalent ID. 
The {0} on the next line is the name of the parameter from the query string to compare to. So you'll need another line of code under the SelectParameters (which I can only imagine defines the parameters for data selection) tags.  After the default ListID parameter you should find something like the following:
Code Snippet

< asp:QueryStringParameter Name="0" QueryStringField="ID">
Notice the Name="0" is the same name between the brackets for the SelectCommand. What this basically does is create a parameter (I like to think of them like a variable) with the value of the ID from the query string. 

No comments: