Multiple Filters on a Data View Webpart

Posted on August 3, 2009. Filed under: Data View Webparts | Tags: , , , |

 Multiple Filters on a Data View Webpart

Well by now if you are playing with the SharePoint List view webparts, its understood that you can have only one data connection associated to a out of box Filter webpart.

And this is where the MAGIC of data view webpart is cast.

This post will guide you through to add multiple filters to a data view webpart.

 Lets start by assuming you have “Sales Contacts” List with Basic Columns like Title, Phone Number, Country, Line of Business, E-mail, etc., And the Requirement here is to have a filter (using drop down controls) on Line of Business & Country.

 In ideal scenario the two columns – Country & Line of Business are Look up columns
(or at least that’s how I would prefer), so in addition to the Sales Contacts we have two more lists in the site by name “Country” & “Line of Business”.

  Open your Site (http://xxxxx/sites/SiteA) in the SharePoint Designer.

  1. Create an aspx page. <Give all the necessary references, styling, webpart zones, etc.> and switch to the design view.
  2. Insert a data view webpart. Choose the “Sales Contacts” list as data source and Insert all the required fields as “Multiple Item View”.
  3. Insert Asp Drop down controls. Switch to code view and set their ID as DropDownFilter1 & DropDownFilter2.              
  4. Now Let’s Data Bound the drop down controls to the Look up lists to fetch the filter values. In the design View Navigate to the data Source Library. Select the “Country” list and click the option “Insert Data Source Control”. A Data Source Control is now inserted on to your page for the resp. list, switch to code view and modify the ID of the datasource to “CountryDS” ( resp. Tag  to identify the data source <SharePoint:spdatasource>). Repeat the same procedure for “Line of Business” list and let the ID be “LineOfBusinessDS”.
  5. Select the DropDownFilter1 and Click “Choose data Source”, assign the “CountryDS” and similarly for the other filter control.

 pic1

 *Do not enable Auto Post back.

6.  Now add a Button and Name it “Filter”. Switch to Code view and add a mouse event. “<asp:Button runat=”server” Text=”Filter”  id=”ButtonFilter” OnClientClick=”javascript: return ApplyFilter(event)/>”.

7. Click on the Data View webpart, from the “Common data view tasks” select the option “Parameters”. Create a new parameter “Filter1”, Select the “Parameter source” as “Control” and “ControlID” as ‘DropDownFilter1’. Similarly create a parameter Filter2 and assign the ControlID ‘DropDownFilter2’.

 pic2

8. Now let’s add the Filter to the Data view web part. From the “Common data view tasks” select the option “Filter”. Enable the “Add XSLT Filtering” and Click Edit. Build the Required XSLT Expression, for e.g. here assuming the look up list has a default item “All” to Filter – [(contains(@Line_x0020_of_x0020_Business, $Filter2) or $Filter2 = ‘All’) and (contains(@Country, $Filter1) or $Filter1 = ‘All’)]”.

9. Now we are left out with the JavaScript function “ApplyFilter”.

<script>

     function ApplyFilter(event)

     {                 

     document.location.href = “http://xxxxx/sites/SiteA/pg/FilterContacts.aspx “;   

     }

</script>

 

 

Alternate Approach 1:

An Alternate approach to apply filter values to the Data view webpart would be by using the Query String parameters. In that case the parameter source would be “Query String” (as discussed in point 7). If you intend to use query strings then the java script function would be –

<script>

function ApplyFilter(event)

{

var DropDownFilter1 = getTagFromIdentifierAndTitle(‘select’,’DropDownFilter1′,”);

var Filter1 = DropDownFilter1.value;

var DropDownFilter2 = getTagFromIdentifierAndTitle(‘select’,’DropDownFilter2′,”);

var Filter2 = DropDownFilter2.value;

if(Filter1 != null && Filter2 != null)

          {

          var url = “http://&#8221; + document.location.host + document.location.pathname;

          document.location.href = url +”?Filter1=” +Filter1 +”&Filter2=” +Filter2 ;

          }

          return false;

}

function getTagFromIdentifierAndTitle(tagName, identifier, title)

{

 var len = identifier.length;

 var tags = document.getElementsByTagName(tagName);

 for (var i=0; i < tags.length; i++)

 {

   var tempString = tags[i].id;

   if (tags[i].title == title && (identifier == “” || tempString.indexOf(identifier) == tempString.length – len))

   {

    return tags[i];

   }

 }      

  return null;

}                          

</script>

 

 

Alternate Approach 2: Using jQuery

If the requirement is that user wants to filter the list by a wild card and if you intend to use jQuery (i’m Lovin it) please refer to this Fantastic utility on Codeplex – Instant List Filters .

Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

12 Responses to “Multiple Filters on a Data View Webpart”

RSS Feed for SharePoint Xperiments Comments RSS Feed

Hi,

Please help me… I have spent hours strugging with this…
I have a dataview web part which takes querystring parameters for filters. I have a Custom Article List which has metadata of Category and Filters. Category and filter are lookups and can have multiple values.

say for example Article A stores data in the following format in sharepoint >
Category – Product Research;Ingredients;Applications
Filter – Cheese; Milk; Dairy;Non-Dairy;Butter;Butter Milk

How can I have the filter conditions setup so that all the possible categories(there are total of 7) and filters(there are total of 11)to filter the Data view to get the right results based on Category and filter Inputs by user2

A custom webpart updates the url with querystring based on the input values selected by the user with parameters say NC1,NC2…NC7 for Categories and NF1,NF2.. NF12 for filters.

Article A should be fetched if it has the metadata of :
1. Category & Filter – Product Research,Cheese
2. Category & Filter – Product Research;Ingredients;Applications,Cheese; Milk; Dairy;Non-Dairy;Butter;Butter Milk
3.Category & Filter – Ingredients,Milk
etc.

Is there some advance Filter formula that can fetch the records for the data view?
I am currently using the condition as below : How can the following be extended to have filters that have values like Butter, Butter Milk, Dairy, Life Saving Dairy etc, since in this case for a “Dairy” input the resultset also fetches articles tagged with Life Saving Dairy.

xsl:variable name=”Rows” select=”/dsQueryResponse/Rows/Row[
(@IWD_x0020_Filter = $Filter or
contains(@Filter, concat(‘;’,$Filter,’;’)) or
starts-with(@Filter,(concat($Filter,’;’)))or
(concat(‘;’,$Filter) = (substring(@Filter,string-length(@Filter) – string-length((concat(‘;’,$Filter)))+1)))) and
(contains(@Category, $Category) and
contains(@Category, $NC1)and
contains(@Category, $NC2)and
contains(@Category, $NC3)and
contains(@Category, $NC4)and
contains(@Category, $NC5)and
contains(@Category, $NC6))]”/>

Hi Mash,
I try to replicate your method but i am having some issue.can you look at the code and pls let me know what i am missing.I tried to use 3 filter but coe down to 1 still cant do when i press the button.pls give some tips to fix.
=========================================

function ApplyFilter(event){
document.location.href = ‘http://Myportal/sites/devSite/FilterPage.aspx’;

}

@FileLeafRef,Name (for use in forms);@Title,Title;@Doc_Area,Policy_Category;@Doc_Group,Doc_Group;@ReviewDate,ReviewDate;@ID,ID;@ContentType,Content Type;@Created,Created;@Author,Created By;@Modified,Modified;@Editor,Modified By;@_CopySource,Copy Source;@CheckoutUser,Checked Out To;@_CheckinComment,Check In Comment;@CheckedOutTitle,Checked Out To;@CheckedOutUserId,ID of the User who has the item Checked Out;@FileDirRef,Path;@FSObjType,Item Type;@HTML_x0020_File_x0020_Type,HTML File Type;@File_x0020_Type,File Type;@IsCheckedoutToLocal,Is Checked out to local;@_SourceUrl,Source Url;@_HasCopyDestinations,Has Copy Destinations;@ContentTypeId,Content Type ID;@_ModerationStatus,Approval Status;@_UIVersion,UI Version;@Created_x0020_Date,Created;@FileRef,URL Path;@File_x0020_Size,File Size;@_UIVersionString,Version;@ParentVersionString,Source Version (Converted Document);@ParentLeafName,Source Name (Converted Document);@TemplateUrl,Template Link;


{F447EC4E-8306-4EAE-A5E2-3AAC47588194}

0

Table

hu

Policy_Tab
Title
Doc_Group
ReviewDate
Created By
Created

ms-alternating

Can you be more detailed here.. if you are following the default approach, make sure you enabled XSLT filtering on the Data view webpart and placed the Filter expression as suggested in Step 8.
Theres nothing much of a code for that approach,, just the button click fn
function ApplyFilter(event){
document.location.href = ‘http://Myportal/sites/devSite/FilterPage.aspx’;
}

Just wanted to say thanks for this – it was exactly what I needed!

nice post.thnx

Thanks for the help………… I really need this
I have done lots of googling but finally found it from here

Great post. Well articulated. Good use screenshots and code.

Thank you for your post. This is what i was looking for. I got one more question. What should i write in the Add XSLT filtering part if i have more than two dropdowns. Lets say i have 5 drop downs. How would i write XSLT filtering?

Once again thank you very much..

Never mind about my previous question. I figured out the way to do that. Thank you very much for your post. You are a lifesaver.


Where's The Comment Form?

    About

    Xperiments n Xperiences. For the Last 5 years I have been providing variuos portal solutions (SharePoint focused) to my clients and now I intend to publish few of those solutions 'Not As It Is', which might help you. – Mash

    RSS

    Subscribe Via RSS

    • Subscribe with Bloglines
    • Add your feed to Newsburst from CNET News.com
    • Subscribe in Google Reader
    • Add to My Yahoo!
    • Subscribe in NewsGator Online
    • The latest comments to all posts in RSS

    Meta

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: