Showing posts with label DataView. Show all posts
Showing posts with label DataView. Show all posts

Saturday, March 19, 2011

Dynamic List Filter Menu Using Data View Grouping

This article explains how to create a dynamic filter menu for content in SharePoint.  Methods from the article SharePoint: List Distinct (unique) values from a specific column in a list are used to obtain unique values from a specific column in a SharePoint list to be displayed as each menu item.  When an filter option from the menu is selected, content on the page will be filtered based on the selection.  

For a single menu displaying filter options dynamically, you only need to associate the menu with one column from the main list.  When an option is selected, the browser is redirected to the same page with a query string parameter set to the selected filter parameter using JavaScript.  The main list on the page is then configured to filter contents based on the parameter.

Components:
  • Document Library with custom column "Heading"
  • One Web Part page with the following components:
    • Data View Web Part displaying the actual documents in the library
    • Data View web Part displaying the possible filter options (from the Heading column)


Usage:
  • When a filter option is selected from the menu Data View, the browser will be sent to the current page with the selected option included as a Query String Parameter (using link or JS).
  • The main Data View web part would be configured with a custom parameter obtained from the Query String.  The custom parameter would be used as filter criteria for the Data View.  The result is that when an option from the dynamic menu is selected, the documents listed in the main Data View will be dynamically filtered based on the selected option.


JavaScript Reference:
The “How To” instructions below refer to JavaScript functions used for Query String Manipulation. To view details about the functions referred to below, as well as other functions for hyperlink/query string manipulation, see:

Query String Manipulation using JavaScript
    - Add Query String Parameter using JavaScript - addParameter()

How To:
  1. Create a new Document Library, or use and existing one if required.
  2. Add a custom column to the library (or content type if using custom content types).  In this example the column will have the title “Heading”, and will be used to list menu options, but it can have any title as required.
  3. From the view menu of the document library or the setting page, select the “Create view” option to add a new view.
  4. When viewing the newly created View page, select the “Edit in SharePoint Designer” from the file menu, or manually open the site and page for editing in SharePoint Designer.
  5. Add a second instance of the same Document Library Web Part to the page above the existing.  Set the crome state to None in the Appearance section if required.
  6. Right click on the new Web Part and select Convert to Data View option.
  7. Once a Data View Web Part, open the Sort and Group dialog, and add criteria to group the list by the “Heading” column.
  8. In the design pane of the page, right click on one of the listed items (documents) and remove the row from the table.  This will remove all list items resulting in only the groups remaining.  Remove additional functionality from the groups, such as the expand, contract button/link or css styles.
  9. Format the group value as a hyperlink, with “#” as the value for the href attribute.
  10. Add an “onclick” attribute, with a call to the addParameter() JavaScript function.  The value of the selected filter option and the parameter name is passed to the JavaScript function which adds the parameter to the current url using a redirect.


If there are no existing parameters on the current page url, the easiest method to configure a hyperlink to add a Query String Parameter to the current page is to set the href attribute to the following:

<a href="?main={$fieldvalue}"><xsl:value-of select="$fieldvalue" /></a>

Using the above (simple) method will replace any existing parameters in the current url with the new parameter and value.  For a single dimensional menu, using this method to add Query String parameter and value to use as filter criteria on other Web Part s on the page would be fine.  For multi dimensional menus (main and sub menu), you may need to retain the parameter in the Query String that determines which main/primary heading when adding the sub-heading filter parameter.  In this case, JavaScript can be used to add the new parameter to the existing Query String.  Using JavaScript to manipulate the query string is a better method to use in either case as existing parameters will always be retain, but would obviously require that JavaScript be enabled on client browsers.  




More Resources for SharePoint Development & Customisation:
 

Tuesday, September 22, 2009

XSLT String Manipulation - Remove spaces in Field Value for use in URL

XSLT function sets the value of a parameter to the value
of the required field, after processing to remove spaces.

The parameter is then used to set part of url in the href attribute of an <a> tag.



<xsl:template name="dvt_1.groupheader0">
<xsl:param name="fieldtitle" />
<xsl:param name="fieldname" />
<xsl:param name="fieldvalue" />
....... other template params

1. Add a parameter to store

<xsl:param name="stringNoSpaces" >
<xsl:call-template name="stringreplace">
<xsl:with-param name="stringvalue" select="@Field-XPath" />
<xsl:with-param name="from" select="string(' ')" />
</xsl:call-template>
</xsl:param>

....... main template code

<a href="/FirstURLComponent/{$stringNoSpaces}"><xsl:value-of select="$fieldvalue" /></a>

....... remaining template code

</xsl:template>


2. Add string replace template after the main template:

<xsl:template name="stringreplace">
<xsl:param name="stringvalue" />
<xsl:param name="from" />

<xsl:choose>
<xsl:when test="contains($stringvalue, $from)"><xsl:value-of select="substring-before($stringvalue, $from)" />
<xsl:if test="contains(substring($stringvalue, 1, string-length($stringvalue) - 1), $from)">
<xsl:call-template name="stringreplace">
<xsl:with-param name="stringvalue" select="substring-after($stringvalue, $from)" />
<xsl:with-param name="from" select="$from" />
</xsl:call-template>
</xsl:if>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$stringvalue" /></xsl:otherwise>
</xsl:choose>
</xsl:template>





The value of the @Field-XPath field will processed by the string replace template to remove all spaces from the string.
The processed string is stored in the stringNoSpaces parameter to be used or displayed by the XSLT template
for a Data View Web Part.

Friday, September 11, 2009

Solved: Can't Group By Choice Column in SharePoint List View

By Default, items in a SharePoint list can't be grouped by a Choice field if it allows multiple selections (checkbox).

To be able to group by the value of a Choice field in a SharePoint list or library which allows multiple selections, you need to ensure that the Choice field/column is set as Radio Buttons or a Drop Down Menu. Both of these options only allow a single selection of the available choices, which is required to allow grouping by the column/field using Standard SharePoint List views.

To group by a Choice field which allows multiple selections (checkboxes), convert the Web Part to a Data View using SharePoint Designer, then apply grouping doing the following:

1. From the "Common Data View Tasks" menu for the Data View Web Part, select "Sort and Group:" from the options to open the Sort and Group Dialog, add any field into the Sort Order section to activate the "Edit Sort Expression..." button. Click the "Edit Sort Expression..." button to open the Advanced Sort Dialog.

2. From the list of fields, add the Choice field, and make sure that the field reference is the only value in the "Edit the XPath expression" input.
The field reference should begin with an @ symbol followed by the internal name
of the SharePoint column/field (example: @Location). The value from the
selected column will display in the Preview box if items in the list/library
match the specified criteria, which in this case is a simple column/field value
(no conditions or calculations required).

3. Press OK on to close the Advanced Sort Dialog. Select the Choice Field from the list of fields in the "Sort Order:" box, then select "Show Group Header" under Group Properties. This will result in items being grouped by the value in the choice field which allows multiple selections.