Wednesday, October 31, 2012

SharePoint list : Get distinct field values from silverlight application

I was in search of any way which will give me distinct field values without using server object model.
I wanted to get distinct field values either by client object model or any other way from client side in my silverlight application.

Many days of search...but no luck :(

Then I decided to do some hacking and focused on the way SharePoint gets distinct field values while displaying filter options on list header.

After bit of hacking using fidler I came to know, SharePoint calls filter.aspx page in Layouts folder and displays it in iframe.
That page calls server side method to get distinct values and displays those values in dropdown.
I decided to write a method in my silverlight application which will request filter.aspx.

Here is the code which I have written. It requests page, gets response in the form of html string, parses string to get field values and adds field values to generic list.


private List<string> fieldValues;

        public void GetDistinctFieldValues(string siteUrl,string listGuid, string viewGuid,string fielInternaldName)

        {
            string requestURL = string.Empty;
            //Form request url
            requestURL = siteUrl+"/_layouts/filter.aspx?ListId="+listGuid+"&FieldInternalName="+fielInternaldName+"&ViewId="+viewGuid+"&FilterOnly=1&Filter=1";

            //Create request instance
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(requestURL));
            request.BeginGetResponse(new AsyncCallback(getFieldValueCallback), request);
        }

 
        private void getFieldValueCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);


            using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))

            {
                string resultString = streamReader1.ReadToEnd();

                fieldValues = new List<string>();

                resultString = resultString.Replace("\"", string.Empty);

                //Regular expression to match with HTML string and get options from Select.

                Match m = Regex.Match(resultString, @"<OPTION Value=s*(.+?)\s*\>s*(.+?)\s*\</OPTION>");

                while (m.Success)

                {
                    //Add field value to generic list

                    fieldValues.Add(m.Groups[1].Value);

                    m = m.NextMatch();
                }
            }
       }
 
Make sure to add references for:
  System.Net
 and  System.Text.RegularExpressions

Usage: GetDistinctFieldValues(http://<myserver>/sites/prasadtest, "{79351260-0F78-44FD-A5B1-1464F961F044}", "{B4A91266-0570-4563-B6C8-221EE543D328}","Title");
Here I am passing site url, list GUID, list view GUID and internal name of the field for which I want distinct values.

 
This code adds field values to generic list named fieldValues.
 
If we want to get it using JavaScript, then also we can achieve this. I am not a JavaScript expert so I am assigning this task to you. Please achieve same thing with JavaScript and post comment to this article.

Please note that this method will not work if your list view exceeds list item threshold limit. So to get this working configure view correctly with appropriate filtering criteria and row limit.