Strident Computer Systems

How to get Distinct or Unique values in a Dataview Web Part

Category: Code Snippets, SharePoint 2007

One of the many requests I have seen from various people is how to get the equivalent of the SQL Distinct query in SharePoint. This actually can be really easy to achieve and I will show you how to do make a dropdown list which only contains unique values.

If you look at my previous post on how to create a dropdown list from a SharePoint list before hand as this article follows directly on.

The section we really need to look at is the following.

<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row" />

This variable holds the dataset that SharePoint Desginer uses for it’s templated DVWP’s, by altering this we can change what information is displayed. To make this work we will need to sort the data on the column we are going to be filtering, in this example I am using the Title field for simplicity.

  • If you click the small cheveron for the DVWP and then click Sorting and Groups.
  • Add the Title Column into the sort order, make sure it’s set to Ascending and click OK.

This will order the Title Column so any repeated entries are listed together in alphabetical order.  The next thing we need to do is to alter the $Rows variable to filter out duplicates from out selection. 

To do this change line noted earlier

<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row[not (@Title=preceding-sibling::Row/@Title)]" />

This XSL will filter out any duplicate entries in the Title Column on your list, you can alter the field that you use but you will need change both of the @Title entries in the Rows variable and remember to sort the webpart by Ascending Order on the column you are using. You will also need to alter the following part of the template to change what values are displayed in the dropdown list.

<xsl:template>
  <option>
    <xsl:attribute>
      <xsl:value-of select="@ID"/>
    </xsl:attribute>
    <xsl:value-of select="@Title" />
  </option>
</xsl:template>

If you need to display your column simply change the @Title to the column you wish to use. I hope you find this useful and hopefully I can post up my Javascript to handle cascading dropdown lists in the future.

8 Responses

  1. larry says:

    I have finally got around to implementing these changes you posted to a request of mine. So I created the dropdown and have unique value. How do I use it to filter a list view?

    thanks

  2. Adam Chamberlin says:

    Hi Larry, one of the ways we found was to use a bit of JavaScript to append a query string variable to the page; http://intranet/default.aspx?dropdownID=1
    From here you can insert a data-view web part and filter it based upon this variable.

  3. Nick says:

    Thank you so much! I have been looking for this resolution for 2 days now on the web…

  4. kenny says:

    Thank you so very much… But I am unable to access your intranet site to the next page… I am having the same requirement to do filter based on this variable selected…

  5. JK says:

    Hi Adam, here in my case i want to show Distinct submitter’s, i replaced @Title with @Author, next i am using count($Row). but it didn’t worked for me. it is showing whole row count.

    can you help me on this please….

    Thanks in advance

    Regards,
    JK

  6. Chuck says:

    Now what…

    This was the best instructions for creating a filtered drop down… but now that I have a web part the way I need it, how do I use that in a form for creating master/detail type data entry.

    However, considering there has been no activity here for a year, I will look elsewhere.

  7. Adam Carr says:

    Generally if you need to create an entry form it’s quicker and generally more reliable to create a custom page in SharePoint Designer, SharePoint 2007 is incredibly picky about editing the 3 main forms and they tend to break. I just used this as a filter for linking custom webparts on pages together.

    Alternatively there is a great jQuery plugin from Marc D Anderson called SPServices, this may well allow you to tailor the form to your requirements as it uses jQuery/Javascript to achieve the changes. Plus Marc has some good articles on his blog on how to customise forms with SPServices.

    http://sympmarc.com#

    I hope this helps,
    Adam

  8. Adam Carr says:

    The @Author column tends to be a bit quirky as it contains alot of extra hard coded information for SharePoint to generate a hyperlink. This may be a bit harder then getting a distinct value on the @Title column because of the extra formatting.

    Marc D. Anderson has some nice XSL templates that might be able to assist in extracting the ID from that column and then you could possibly get a disctinct value from that.

    http://spxslt.codeplex.com/documentation

Leave a Reply