Monday, June 10, 2013

Get unique values from SharePoint field

In this post we are going to see how we can get the unique values from a specific field from SharePoint list.

Method is very simple. All you have to do is use this code and you can get the distinct values from list field.


            SPList list = SPContext.Current.Web.Lists[lstGUID];

            SPField field = list.Fields.GetField("OrderDate");  //OrderDate is date field. Replace with the field that you want to get unique value of.

            object[,] values;
            uint numberValues = list.GetDistinctFieldValues(field, out values);

            for (int i = 0; i < numberValues; i++)
            {
                lblValues.Text += values.GetValue(0, i).ToString() + "
";
            }

Variables and labels are used only for reference.

GetDistinctFieldValues is a method which gives us unique values that gets stored in the object array which you can iterate and can get the values.

However this method is obsolete. but just in case this can becomes a day saver some time. 

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com