6

How can I retrieve all elements in a List ignoring duplicate entries

So if List A contains:

Employee A
Employee A 
Employee B 
Employee B
Employee C 
Employee D   
Employee E

I want to get

Employee A
Employee B 
Employee C 
Employee D
Employee E
DanielR
  • 171
  • 1
  • 1
  • 2
  • 1
    This is a really unclear question, and the answers you are likely to get are going to be all over the map. If you explain your requirement in a little more detail you are likely to get more useful answers. Who knows, maybe you can achieve what you want without using CAML at all! – Derek Gusoff Sep 25 '13 at 14:32
  • ...and please include the SharePoint version you are using...that matters a lot. – Derek Gusoff Sep 25 '13 at 14:32

2 Answers2

10

Daniel,

The Distinct operation isn't available in CAML Queries.. However, there are different things you can try.. You can use LINQ to get distinct results:

var distinctItems = (from SPListItem item in items select item["EmployeeName"]).Distinct().ToArray();

Or convert your results to DataView and do something like:

SPList oList = SPContext.Current.Web.Lists["ListName"];
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");
Arsalan Adam Khatri
  • 14,531
  • 3
  • 36
  • 59
  • @Rinu pls have a look at this answer https://sharepoint.stackexchange.com/questions/27212/how-to-retrieve-distinct-values-with-javascript-client-object-model – Arsalan Adam Khatri Mar 19 '18 at 06:14
8

CAML query schema for SP2013 doesn't contain distinct still, so I think it is not changed. However you may group results in CAML (using GroupBy element) - it will produce groups for distinct values of the field.

Ref: http://social.technet.microsoft.com/Forums/sharepoint/en-US/c2bbcf13-a53d-4755-87dd-871a51840a4d/select-distinct-in-caml-query-for-list

Karthik Jaganathan
  • 7,848
  • 4
  • 34
  • 51