Friday 22 June 2012

Get distinct values in share point 2010 list using CAML query

Get distinct values in share point 2010 list using CAML query


Step 1


Create a list in Sharepoint 2010 site, here I have created a list called "CAMLTEST" and entered three items in to the list



Here the Name "Rajesh" is repeated.

Step 2


I have created a visual Web part with a label - Name, drop down - ddl_names and a button - "btn_binddropdown". It will show the distinct values from the list.

Step 3


Write in the btn_binddropdown button click event...


 protected void btn_binddropdown_Click(object sender, EventArgs e)
        {
            try
            {
                SPList oList = SPContext.Current.Web.Lists["CAMLTEST"];
                SPQuery query = new SPQuery();
                query.Query = "<OrderBy><FieldRef Name='Name' /></OrderBy>";
                DataTable dtcamltest = oList.GetItems(query).GetDataTable();
                DataView dtview = new DataView(dtcamltest);
                DataTable dtdistinct = dtview.ToTable(true, "Name");
                ddl_names.DataSource = dtdistinct; 
                ddl_names.DataTextField = "Name";
                ddl_names.DataValueField = "Name";
                ddl_names.DataBind();                           
                               


            }
            catch (Exception exe)
            {
                Response.Write("Exception at btn_binddropdown_Click :" + exe);
            }
       }




Step 4


Deploy the webpart to the sharepoint site required (the site should contain the "CAMLTEST" list).

Insert the wepart in to any page and click the binddropdown button, the dropdown will be populated with the distinct values of name column in CAMLTEST list.








Happy Coding :))))))))))))))





































15 comments:

  1. im new to development side
    using caml query i need to show list of names
    for eg:
    entering 'R'
    it should display names starting with 'R'.
    can you send me the solution

    ReplyDelete
    Replies
    1. Hi Sweety, I need to confirm that which control you are using..

      ASP DropDownlist or any third party controls.

      If you are using the telerik rad controls its very easy.

      If you are using asp.net dropdownlist there is no event called item_requested.

      Delete
  2. Thanks...vry useful info

    ReplyDelete
  3. How would you use the distinct selector of an ID field, but display the NAME field?

    ReplyDelete
    Replies
    1. I am not using ID field here, I am using name field to get distinct names.

      Delete
  4. This is asinine. You're really going to dump it to a dataview to get the distinct values? What is wrong with you, that doesn't meet the use case at all, and it's impossibly heavy, that's like using a Mack truck to tow a big wheel. If you're going to do that limit your query to returning only the distinct comparable and use Linq to call .distinct on the collection. Jeez.

    ReplyDelete
  5. Neatly written and clearly explained! Thank you. :)

    ReplyDelete
  6. Thanks very much by mayank tiwari 08/08/2013

    ReplyDelete
  7. Thank you Kay for the suggestion.

    ReplyDelete
  8. Hello,

    Kay, I  totally agree with you

     

    this topic not use but really not use camel query to group (distinct )there items but use dotnet dataview after processes query but ok for small data

    My question is  :

    when we want to retrieve big data from large document library ,with ex: nice SPListItemCollectionPosition  dot net object ,

    because  foreach paging you process new query ,

    how to distinct this with dataview ??.

     

    ReplyDelete