Yes! You can add multiple filters to a List view webpart

Posted on September 18, 2009. Filed under: Data View Webparts | Tags: , , , |

Have you recently tried to apply filters from the Column Header dropdowns in any list!!!!

(After the SP2, the Column header drop downs now have the unique list field values to select and filter list items)

Try it, and see the reflected URL in the browser –

Something like this – “http://xx /test/SalesContactNewUI_Test.aspx?View={FB06F5EA-EDD3-433A-9FF4-C2CBEB88FF60}&FilterField1=Line%5Fx0020%5Fof%5Fx0020%5FBusiness&FilterValue1=Auto%20Dealer%20Commercial%20Services&FilterField2=Country&FilterValue2=Europe”

The highlighted parameters in the above URL can be utilized to apply a filter on any List view webpart.

Let’s pick the same business scenario as in my previous post https://sharepointxperiments.wordpress.com/2009/08/03/multiple-filters-on-a-data-view-webpart/ .

Get your dropdowns data bounded to the look up lists, add the resp list webpart to the page and add the JavaScript method for the button click event.

Though it’s a pretty quick way of doing, we would miss the flexibility data views provide.

JavaScript –

function ApplyFilter(event)

{

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

var Filter1 = DropDownFilter1.value;

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

var Filter2 = DropDownFilter2.value;

var url = “http://” + document.location.host + document.location.pathname;

              if(Filter1 != “All ” && Filter2 != “All “)

             {

  document.location.href = url +”?View={FB06F5EA-EDD3-433A-9FF4-C2CBEB88FF60}&FilterField1=Line%5Fx0020%5Fof%5Fx0020%5FBusiness&FilterValue1=” +Filter1 +”&FilterField2=Name&FilterValue2=” +Filter2 ;

             }

             if(Filter1 != “All ” && Filter2 == “All “)

            {

   document.location.href = url +”?View={FB06F5EA-EDD3-433A-9FF4-C2CBEB88FF60}&FilterField1=Line%5Fx0020%5Fof%5Fx0020%5FBusiness&FilterValue1=” +Filter1 ;

              }

             if(Filter1 == “All ” && Filter2 != “All “)

            {

   document.location.href = url +”?View={FB06F5EA-EDD3-433A-9FF4-C2CBEB88FF60}&FilterField1=Name&FilterValue1=” +Filter2 ;

              }

             if(Filter1 == “All ” && Filter2 == “All “)

 {

 document.location.href = url;

 }

 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;

}

Advertisements
Read Full Post | Make a Comment ( 2 so far )

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 .

Read Full Post | Make a Comment ( 12 so far )

Reusable Data View Webpart

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

Well if you ponder on how to re use a certain data view webpart across various sites, this would be the way to do. Use Lists.asmx as the ‘XML web services’ data Source for your data view webpart.

Let’s say the Organization Announcements are at the ‘Root Site A’, you want this webpart across SiteB & SiteC.

ReUse1

Brief Steps:

  1. Create a new aspx page in SiteB. Provide all the required master page references, styling and Insert a SharePoint web part zone.
  2. Now Insert a Data View and Click to select the Data Source.
  3. On the Data Source Library pane, Choose “XML Web Services” and Click ‘Connect to a web service’.
  4. Modify the Data Source Properties. Provide the appropriate Lists.asmx web service of the Root Site A. (http://xxxxxx/RootSiteA/_vti_bin/lists.asmx) and connect.
  5. Choose appropriate Command (Select), Operation (GetListItems) and Modify the ‘List Name’. Provide the List Name as ‘Announcements’ and Ok.
  6. A data Source is now created using Lists.asmx on Announcements from Root Site A.
  7. Now Select the option “Show Data” and Insert the selected Fields as “Multiple Item View”. Save the Page.
  8. Cool, now that you have the required webpart in your SiteB, lets reuse it in SiteC. Open the page from your browser and Edit the page.
  9. Select the edit properties of the data view webpart and click “Export” to save the ‘xxxx.webpart’ on your local file system.
  10. Upload this ‘xxx.webpart’ to the webparts gallery of SiteC and it will be available for you to add in on any page.

 Additional Customizations:

If you have any additional customizations via JavaScript or css on this data view webpart include these within the XSL style sheet by creating a new xsl template. This way when you export the webpart the respective customizations will be stored in the ‘xxxx.webpart’.

<xsl:template>

<xsl:text disable-output-escaping=”yes”>

<![CDATA[   

<Script></script>

]]></xsl:text>

</xsl:template>

.

.

<xsl:call-template name =”script”></xsl:call-template>

Read Full Post | Make a Comment ( 2 so far )

    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...