Monday, June 8, 2009

Using URL to filter Web Parts on a SharePoint Page





Adding URL Query to filter Web Parts
The point of this walk through is to explain how to create filters for web parts based on the page URL. Web parts to be filtered must be XSLT Data View Web parts. One of the fields that exist in the web parts corresponding list or library must contain data that matches the page name. If filtering multiple web parts on a page you must complete the following steps for each one. !!Note that some performance issues may arise if your filter has to process through thousands and thousands of records!!

  1. Add Web Part(s) to page
  2. Open page in SharePoint Designer
  3. Right Click on Web Part and Select Convert to XSLT Data View
  4. Select the Common Tasks
  5. To remove the tool bar select Change Layout
  6. Go to the General Tab
  7. Uncheck any boxes to remove the toolbar and filtering
  8. Click Ok
  9. Select Common Tasks
  10. Select Parameters…
  11. Enter New Parameter
  12. Enter CurrentUrl
  13. Select Server Variable as Parameter Source
  14. Enter URL as Server Variable
  15. Click Ok
  16. Select the Common Tasks
  17. Select Filter
  18. Check Add XSLT Filtering
  19. Click Edit
  20. Enter the following code and/or adapt it to your use[@Title=substring-after(substring-before($CurrentUrl,'.aspx'),'/Pages/')]
    Code Explanation
    @Title = the field that contains the content that matches your page name (i.e. ProductA is an item in the Title field and ProductA.aspx is the page)

    $CurrentUrl = the parameter we just created

    /Pages/ = the document library the pages reside in (if your pages reside in a document library named Products set this to /Products/)

    The query pulls out the red information to use as the filter criteria http://sharepoint/pages/page2.aspx and matches it against the field chosen.
  21. Click Ok
  22. Click Ok
  23. Save your page, you will notice at this time that your web parts return no results, the results will not show because the filter only processes at runtime.
  24. Refresh the page

Actual Code that is added to the Page is as follows:

In Parameter Bindings
parameterbinding location="ServerVariable(URL)" defaultvalue="" name="CurrentUrl"

In XSL Style Sheet
xsl:param name="CurrentUrl"

In xsl:template name="dvt_1"
xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row[@Title=substring-after(substring-before($CurrentUrl,'.aspx'),'/Pages/')]"

Slacking latley

I have been slacking lately with adding items to this blog, my wife and I just recently had a baby and of course I lost track of other items I have been working on, but I am on the upswing again so items should start popping up again.