DataZoom
Get Distinct Column Values
See Also Send Feedback
Samples > Get Distinct Column Values

Glossary Item Box

Learn how to get a list of distinct column values of a list. The column values are displayed in a drop-down list and posted back to the page using a query string variable when selected. An if/else condition is used to display the results of the selected item.

Prerequisites

This example requires a SharePoint list named 'City' to exist in the current site. The 'City' list must have two fields: Title and State. In this example the State field is a single line text value limited to 2 characters.

 

Syntax

Get List Items Using a CAML Query Copy Code
## Get the orders list
#set( $list = $web.lists.get_item("City") )    

## Get the distinct states
#set( $states = $SPUtility.GetDistinctFieldValues($list, "State") )

## Get the selected state from the query string
#set( $selectedState = $queryString.get_item("State") )

## Get the current URL
#set( $url = $serverVariables.get_item("URL") )

<br>
Filter orders by selecting a state below...
<br><br>
<
select id="stateFilter" name="stateFilter" onchange="window.location = this.value;">
<option value="$url">Choose a State...</option>
#foreach($state in $states)
           #each
 
<option value="$url?state=${state}" #if($selectedState == $state) selected #end>${state}</option>
           
#nodata
        
<option value="$url">No states found</options>
    
#end
</select>

Output

See Also

©2010 SharePoint Solutions. All Rights Reserved.