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.

16 Comments

  1. Nice t know that somewhere is there waiting for it!  I’ll try to get something posted in the next few days.  Here’s a hint: you need an onclick event on the select input…
    Reply
  2. Any idea when you will post "How to make something happen when the user selects a value from the dropdown?"  I have been trying to use web connections to allow a user to select a value that filters a dvwp – no problem with the filtering functionality except if I choose the dropdown style for the source list.  This style would be ideal because the source list is rather long and takes up a lot of realestate.  When I choose the dropdown, the user can select a value from the list just fine, but nothing happens to the destination web part (no filtering as a result).  Feels like I need a "Go" button or something.  Looking forward to your comments…
    Reply
  3. 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?

    Reply
    • 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.

      Reply
  4. 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

    Reply
    • 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.

      Reply
  5. 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

    Reply
  6. 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.

    Reply
    • 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.

      Reply
  7. 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,

    Reply

Have a thought or opinion?