SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have got a list with names of workers. How could I populate a DropDownList with unique values (ie: avoid duplicate) from the list? If the list contains duplicate values I need to avoid inserting the same value multiple times in the dropdown.

Here is my current code:

SPList kontraktyList = witryna.Lists["Urlopy_prac"];
SPQuery kontraktyQuery = new SPQuery();
kontraktyQuery.Query = "<Query><OrderBy><FieldRef Name='nazwisko'/></OrderBy></Query>";
SPListItemCollection kontraktyGrupa = kontraktyList.GetItems(kontraktyQuery);

foreach (SPListItem kontraktyListItem in kontraktyGrupa)
share|improve this question
Hi Grzegorz - I don't think you want <Query> in your Query... whenever I put that in my CAML it stops it working and just returns all results without any mind paid to the query. – TZHX Dec 12 '12 at 13:04
I want only take one field from the External List. How Would it be fasters, and oriented Ascending ? Only one field, but ordered Ascending – Grzegorz Z Dec 12 '12 at 13:25
If you want your CAML query to return only 1 field, please use: kontraktyQuery.ViewFields = "<FieldRef Name='nazwisko'/>";. No sense to use the .Query property to restrict the number of fields to be returned. The .Query property is for <Where/> and <OrderBy/> clauses. – Norbert Dec 12 '12 at 14:11
up vote 1 down vote accepted

I would modify your foreach like this:

foreach (string kontraktyListText in kontraktyGrupagreat.Cast<SPListItem>().Select<SPListItem,string>(source => source["nazwisko"] as string).Distinct())

Please try it out and tell me if it worked.


share|improve this answer
It's no worked on my Code :( Please help. Meybe in element it should be ? – Grzegorz Z Dec 12 '12 at 13:20
Want to go to chat?… – Norbert Dec 12 '12 at 13:37
I'm on chat :) :) – Grzegorz Z Dec 12 '12 at 13:42
Ok, I am sorry, I made a stupid mistake. I have edited my response. – Norbert Dec 12 '12 at 14:06
Now its a great solution :) – Grzegorz Z Dec 12 '12 at 14:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.