Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

SPList.GetDistinctFieldValues Method

Windows SharePoint Services 3

Returns a two-dimensional object that represents an array of field values in which data duplication within a specified field in the list is eliminated and each value returned is unique.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public uint GetDistinctFieldValues(
	SPField field,
	out Object[,] columnItems
)

Parameters

field
Type: Microsoft.SharePoint.SPField
A SPField object that represents the field from which the duplicates are to be removed.
columnItems
Type: [,]
A two-dimensional object array that is passed as an out parameter and that returns the values.

Return Value

Type: System.UInt32
Returns an UInt32 An unsigned 32-bit integer that specifies the number of field values represented in the array.

ExceptionCondition
ArgumentNullException

Field is null.

Exception

Can not get distinct values.

Field Type is not text, number, currency, DateTime, or Boolean.

The GetDistinctFieldValues(SPField, [,]) method can only be used for Text fields.

The following code example uses the GetDistinctFieldValues(SPField, [,]) method to return a two-dimensional object array representing values from the Title field of a specified list and then iterates through the array to display unique values.

Visual Basic .NET

Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim list As SPList = siteCollection.AllWebs("Site_Name").Lists("List_Name")
Dim field As SPField = list.Fields("Title")

Dim columnItems(,) As Object

Dim numberValues As System.Int32 = Convert.ToInt32(list.GetDistinctFieldValues(field, columnItems))

Dim i As Integer

For i = 0 To numberValues - 1

    Dim uniqValue As String = SPEncode.HtmlEncode(columnItems.GetValue(0, i).ToString())

    Label1.Text += uniqValue & "<BR>"

Next i 

C#

SPSite siteCollection = SPControl.GetContextSite(Context);
SPList list = siteCollection.AllWebs["Site_Name"].Lists["List_Name"];
SPField field = list.Fields["Title"];

object[,] columnItems;

uint numberValues = list.GetDistinctFieldValues(field, out columnItems);

for (int i=0; i<numberValues; i++)
{
    string uniqValue = SPEncode.HtmlEncode(columnItems.GetValue(0, i).ToString());
    Label1.Text += uniqValue + "<BR>";
}

This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint, Microsoft.SharePoint.Utilities, and Microsoft.SharePoint.WebControls namespaces.

The example assumes the existence of an .aspx page that contains a label control.

Community Additions

ADD
Show:
© 2015 Microsoft