Eliminating Duplicates in Data View Web Parts
A Data View Web Part (DVWP) that displays a dropdown can be a useful tool to drive navigation. See my previous post about showing content archives (Displaying an Archive for a SharePoint List) as an example.
If you have a decent number of items in the list you are using, you will likely end up with duplicates in your dropdown, which isn’t really what you want. Here’s the trick to remove the duplicates.
When you add your DVWP, add your column for the dropdown, and set your DVWP layout to a dropdown view type, you will get a chunk of code like the following:
<xsl:template name="dvt_1.rowview"> <option> <xsl:attribute name="value"> <xsl:value-of select="@Your_x0020_Column" /> </xsl:attribute> <xsl:value-of select="@Your_x0020_Column" /> </option> </xsl:template>
Find this code, and change it as follows:
<xsl:template name="dvt_1.rowview"> <xsl:variable name="NewGroup" select="ddwrt:NameChanged(string(@Your_x0020_Column), 0)" /> <xsl:if test="string-length($NewGroup)"> <option> <xsl:attribute name="value"> <xsl:value-of select="@Your_x0020_Column" /> </xsl:attribute> <xsl:value-of select="@Your_x0020_Column" /> </option> </xsl:if> </xsl:template>
The ddwrt:NameChanged function will return a value only when the value of the column has changed since the last new value.
Next up: How to make something happen when the user selects a value from the dropdown.
Never mind. I got it to work using your alternative method instead. Thanks for sharing your method in creating a DVWP dropdown.
Glad you got it working!
M.