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 :))))))))))))))
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 :))))))))))))))
im new to development side
ReplyDeleteusing 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
Hi Sweety, I need to confirm that which control you are using..
DeleteASP 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.
Thanks...vry useful info
ReplyDeleteWelcome
DeleteHow would you use the distinct selector of an ID field, but display the NAME field?
ReplyDeleteI am not using ID field here, I am using name field to get distinct names.
DeleteThis 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.
ReplyDeleteNeatly written and clearly explained! Thank you. :)
ReplyDeleteWelcome
DeleteThanks very much by mayank tiwari 08/08/2013
ReplyDeleteWelcome
Deletethank you:)
ReplyDeleteWelcome
DeleteThank you Kay for the suggestion.
ReplyDeleteHello,
ReplyDeleteKay, 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 ??.