PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Approaches when customizing a DataView WebPart (DVWP)

Posted by PANVEGA on February 7, 2009

In this post I wanna show some tricky approaches when customizing  DataView Webparts (DVWP) in the SP Designer. Maybe the approaches can save you a lot of time. Actually you can customize the weparts view how ever you wish. Customize the Filtering, Grouping and Sorting values in the dvt_1.toolbar template. Present the information with your own CSS classes. Replace the default .ms-toolbar class in the core.css.

Creating the initial dataview can be done on a virtual machine, so there is no risk of breaking anything (and development is easier when you don’t have to deal with the VPN). In SharePoint Designer, create a dataview on any list and convert it to xsl. Don’t make any changes before the conversion, as the conversion doesn’t always work that well.

Introduction:

Microsoft Windows SharePoint Services provides the powerful Data View Web Part that can perform an Extensible Stylesheet Language Transformation (XSLT) on XML data retrieved from a data source. When you are working with a Web site based on Windows SharePoint Services from within Microsoft Office FrontPage 2003, you can use the Data View Web Part to do the following:

  • Define a query and data source from which to retrieve the XML data. Data sources can be SharePoint lists or external databases such as Microsoft SQL Server.
  • Define the XSLT transformation that converts XML retrieved from the data source into HTML. FrontPage offers a WYSIWYG experience for editing these XSLT views, including live data preview.

One of the first things to mention is that although FrontPage is a great tool to create a DVWP, all the logic is already available in SharePoint (WSS) itself . This logic consists of the following parts:

  • Microsoft.SharePoint.WebPartPages.DataViewWebPart, a class available in the Microsoft.SharePoint.dll assembly that implements the DVWP functionality.
  • Microsoft.SharePoint.WebPartPages.DdwRuntime, an internal class available in the Microsoft.SharePoint.dll assembly that is made available as a XSLT Extension Object that provides a set of functions in the ddwrt namespace. The set of functions this article is about!!
  • Data view web part definition, a .dwp file describing the assembly implementing the web part and the configuration parameters for the web part.

It is possible to create data DVWPs without using FrontPage. It is difficult, but it is possible. A DVWP is just a .dwp file which is an XML file that contains information for the data query and the XSLT transformation.

Some customizing approaches

– One first thing when working with the DataView Webpart is how to customize the filter appearence. To show up the filter when loading the Webpart, you have to add the filter string in the Stylesheet parameters. By default  you have to open the filter dropdowns which is not the best solution.

<xsl:param name=”dvt_adhocmode” >filter</xsl:param>

– Locate the dvt_1.toolbar template and toward the bottom of it you’ll find an xsl:if test on the dvt_adhocmode variable.  The sections that follow determine which Selects you will see when you click the Filter link.  I just removed the ones I didn’t want. If your filter value does not appear automatically, you can add it manually like:

<th nowrap=”nowrap”>
<table>
<tr>
<td class=”ms-toolbar” nowrap=”nowrap”>
<xsl:call-template name=”dvt.filterfield”>
<xsl:with-param name=”fieldname”>language</xsl:with-param>
<xsl:with-param name=”fieldtitle”>Language</xsl:with-param>
<!–fieldtitle is the DD Labelname–>
<xsl:with-param name=”Rows” select=”$Rows” />
<xsl:with-param name=”fieldtype”>text</xsl:with-param>
</xsl:call-template>
</td>
</tr>
</table>
</th>

– If you do not wanna show the “ALL” or “EMPTY” values in the filter, you can delete it in the dvt.filterfield template.

– Here  in this template <xsl:template name=”dvt.filterfield”> you also find the XPATH queries retrieving unique values for the DropDown menu.

<xsl:choose>
<!–Custom Filter Values–>

<xsl:when test=”$fieldname  = ‘coursestart'”>
<xsl:variable name=”dvt_Rows”>
<xsl:for-each select=”$Rows”>
<xsl:sort select=”*[name()=$fieldname]” order=”ascending” data-type=”{$sorttype}” />
<!–above getting all values from the rows sorted –>
<xsl:copy-of select=”.” />
</xsl:for-each>
</xsl:variable>
<xsl:for-each select=”msxsl:node-set($dvt_Rows)/*[not(*[name()=$fieldname]=preceding-sibling::*[1]/*[name()=$fieldname])]/*[name()=$fieldname]“>
<!–above retrieving  distinct values from the dvt_Rows Array variable–>

<xsl:sort data-type=”{$sorttype}” />
<xsl:call-template name=”dvt.filteroption”>
<xsl:with-param name=”name” select=”$fieldname” />
<xsl:with-param name=”value” select=”ddwrt:FormatDate(string(.),3079 ,1)” />
<xsl:with-param name=”type” select=”$fieldtype” />
</xsl:call-template>
</xsl:for-each>
</xsl:when>
…….

The template checks the type format of the parameter. Here I directly formated the date value. So actually you can skip calling the template and put the DropDown <option/> directly in this template.


– One of the columns I chooswd to show in my data view was a ContentType. Locate the dvt.filterfield template in the Xsl and locate the following xsl:for-each statement:

<xsl:for-each select=”msxsl:node-set($dvt_Rows)/*[not(@*[name()=$dvt_FieldNameNoAtSign]=preceding-sibling::*[1]/@*[name()=$dvt_FieldNameNoAtSign])]/@*[name()=$dvt_FieldNameNoAtSign]”>

All I did to fix it was remove the [1], like this:

<xsl:for-each select=”msxsl:node-set($dvt_Rows)/*[not(@*[name()=$dvt_FieldNameNoAtSign]=preceding-sibling::*/@*[name()=$dvt_FieldNameNoAtSign])]/@*[name()=$dvt_FieldNameNoAtSign]”>

– To make the dataview configurable you will need to make some minor changes to the parameters it takes. By default the dataview is associated with a list using a guid which will not work on another site.

<SelectParameters><asp:Parameter DefaultValue=”{9B7A603A-466F-474B-9509-BB8423823525}” Name=”ListID”></asp:Parameter></SelectParameters>

This needs to be changed to

<SelectParameters><WebPartPages:DataFormParameter Name=”ListName” ParameterKey=”ListName” PropertyName=”ParameterValues” /></SelectParameters>

In the browser, use export from the web part menu to produce a web part file that can be added to the web part gallery of the real site. You now have a dataview web part that can be added to a page without having to use SharePoint Designer.

Although the web part definition does have a list of included columns that will only match the list it was originally created for, they appear to be ignored when you connect it to another list. That means you have complete control of the dataview using the parameters and xsl available on the tool pane.

How to show the DropDown Selection always on top:

It can be annoying when the filter option gets invisible each time you select a filter value:

Solution:

– go to the <xsl:template name=”dvt_1.toolbar”> template

– at the end of this template you find a if statment

<xsl:if test=”$dvt_adhocmode = ‘filter'” ddwrt:cf_ignore=”1″>….. </xsl:if>

delete this statement and what a suprise your filter DropDowns are visible all the time, Great!!!

Specification:

In the DataView Properties you can add Filter, Grouping and Sorting. By default the XSLT quiries the datasource and requests the unique Values from this particular field and shows these values in a dropdown control. However I do not wanna have all these values in the dropdown. I show how to add static values to the DataViews.

Now I show where you have to change the XSL  code for a static Dropdown:

dropdown

<xsl:template name=”dvt.filterfield”>

…..

<option value=”##dvt_empty##”>
<xsl:if test=”$dvt_filterfield=$fieldname and $dvt_filterval=””>
<xsl:attribute name=”selected”>true</xsl:attribute>
</xsl:if>
(Empty)</option>

<option value=”##dvt_all##”>
<xsl:choose>
<xsl:when test=”not($dvt_filterfield)”>
<xsl:attribute name=”selected”>true</xsl:attribute>
</xsl:when>
<xsl:when test=”$dvt_filterfield!=$fieldname”>
<xsl:attribute name=”selected”>true</xsl:attribute>
</xsl:when>
</xsl:choose>
(All)</option>

<xsl:value-of select=”ddwrt:NameChanged(”,1)” />
<xsl:choose>
//static Values
<xsl:when test=”$fieldname  = ‘coursename'”>
<option value=”Training Module-1″>Module-1</option>
<option value=”Training Module-2″>Module-2</option>
<option value=”Training Module-3″>Module-3</option>
</xsl:when>

….

</xsl:template>

Note: the value “Training Module-X” is in your datasource and “Module-X” is only for the View. Otherwhise you will not receive any results.

In this template you have to catch your DD value in a if -statment, otherwise you do not get your static DD.

Attention!!!

However this has one disadvantage, the value you wanna filter sets to the default usually to (All), so you never know what you have filtered.

Check the $dvt_filterfield value and set the appropriate index to select = true. That is.

<option>
<xsl:attribute name=”value”>
<xsl:value-of select=”‘1′” />
</xsl:attribute>
<xsl:if test=”$dvt_filterfield=$fieldname and $dvt_filterval=normalize-space(‘1’) and $dvt_filterval!=””>
<xsl:attribute name=”selected”>true</xsl:attribute>
</xsl:if>
<xsl:value-of select=”‘german'” />
</option>
<option>
<xsl:attribute name=”value”>
<xsl:value-of select=”‘2′” />
</xsl:attribute>
<xsl:if test=”$dvt_filterfield=$fieldname and $dvt_filterval=normalize-space(‘2’) and $dvt_filterval!=””>
<xsl:attribute name=”selected”>true</xsl:attribute>
</xsl:if>
<xsl:value-of select=”‘english'” />
</option>


However I still have some open questions:

  • How is it possible to apply  a multiselect DropDown. However a selection of Filtering, Grouping and Sorting is already possible?
  • Is it possible to change the hardcoded option value in the DropDown against values with operators (e.g. Training* or Value greater than)?

I would really appreciate when someone has an answer to my questions?

If you know more interesting ways how to customize the DVWP, please let me know.

Thanks

More Informations:

http://msdn.microsoft.com/en-us/library/aa505323.aspx

http://weblogs.asp.net/soever/archive/2005/01/03.aspx

http://www.sharepointcustomization.com/resources/whitepapers/webpartdocs/dataview_wp.doc

http://blogs.msdn.com/sharepointdesigner/archive/2007/04/24/spdatasource-and-rollups-with-the-data-view.aspx

http://www.tonstegeman.com/Blog/Lists/Posts/Post.aspx?List=70640fe5-28d9-464f-b1c9-91e07c8f7e47&ID=85

http://www.codeproject.com/KB/sharepoint/DataViewAttachments.aspx

Advertisements

7 Responses to “Approaches when customizing a DataView WebPart (DVWP)”

  1. VENKAT said

    Sir,

    I have got this working. Found that, filtering on . 1 drop down seems not to work.

    when a choice is made on a second drop down,the filter on first drop down gets invalidated.

    Is there a way to accompolish this?

    thanks,
    venkat

  2. The topic is quite trendy on the Internet right now. What do you pay attention to when choosing what to write ?

  3. PANVEGA said

    This was one of a customer request. So I decided to list some tricky parts when creating such a custom data view part. Making things easy!!!

  4. EvilGenius said

    Have you had success in moving these webparts to a visual studio 2005 project. Just curious as I have tried unsuccessfully to accomplish that.

  5. Kyle Stevenson said

    I’ve created a Data View, but when I select Data View Properties, under the “Show Toolbar With Options For” area there are only two options: One for sorting and one for grouping.

    I have a basic table layout. I’m using SPD 2007.

    Any idea’s where the “Filter” option is?

  6. Prakash Chidambaram said

    For removing duplicates in filter’s drop downlist

  7. Maarten Sundman said

    I think this would be a cleaner method of getting unique filter values:

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

 
%d bloggers like this: