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.
Marc where is the dropdown you used here placed?
Is it displayed as a list item or is it out of the list and used to filter a list.
I dragged my date field to a DWP i want to use to filter but how can i trun it to a dropdown list?
Any ideas?
Patrick:
Reading back through it, this post is really focused on how to eliminate the duplicates, but not on creating the dropdown (select). Let me add a new post explaining the steps to go through to create the dropdown in the first place.
M.
Marc:
I have tried to use it but i did not get the perfect result, I have some 15k records and out of them 32 are dintinct , but when i use this code am able to see some duplicate values in my drop down list.
can u help me int his regard
Thanks,
Prashanth
Prashanth:
Make sure that your items are sorted by the column for which you want to eliminate the duplicates. All the ddwrt:NameChanged function does is compare the current item with the last one. If your items aren’t sorted, you’ll still get plenty of duplicates.
To do this, add an xsl:sort clause to your dvt_1.body template.
M.
Hi Mark I got a question regarding name change how can i
pass the value of a variable which holds the name of the attribute
is it possible? I want to make a generic template for any column in
row variable name=”ColumnName” select=”@Machine” machine is an
column of row. ddwrt:NameChanged({$ColumnName},0) I tried this but
it seems not working. Regards Lito
Alec:
You can make the column name(s) variable by doing something like this:
Here’s a post I did which shows this in more detail:
http://sympmarc.com/2008/12/16/sorting-displays-in-data-view-web-parts/
M.
Hi Marc,
Thanks for writing valuable posts .I am very glad to say that your posts are very helpful and I often like to read your blog.But I feel very happy if you come up with more and more screenshots ,the purpose of its etc.
I would like to get distinct count of @Author field..
saran:
Person or Group columns are tricky because they contain a chunk of markup which is unique for each occurrence on the page. If you are in SharePoint 2010, you can use @Author.id and do your counts on that.
M.
Thanks Mark, I was looking for it.
I did everything to the T. Created a DVWP with one column (assigned to), sorted by assigned to, removed the paging limitation, change DVWP to dropdown, inserted the 3 lines of code you showed above. The dropdown only displays the first Assign To, not the rest. Tried also on other fields, same result. Without the 3 lines of code, all assigned to displays are showing , sorted. What am I doing wrong? Why is the first distinct record showing in dropdown and not the rest?
Regards,