0

I want to retrieve the existing field groups (from root) and display them in a dropdown list.

I'm using this code to retrieve all the columns (and display which group they're belonging to):

 var web = clientContext.Web;

                FieldCollection rootFields = web.Fields;

                clientContext.Load(
                    rootFields,
                     fields => fields
                      .Include(field => field.Group)
                   );
                clientContext.ExecuteQuery();

                foreach (Field _fields in rootFields)
                {
                    fieldsList.Add(new SelectListItem { Text = _fields.Group });
                }

This shows a few hundred groups (duplicates ofc), I want to narrow it down to just the few groups that exist, and sort out the duplicates. Or is there another way to do this?

Robin
  • 740
  • 3
  • 12
  • 30

1 Answers1

0

You can do it via Linq:

Distinct will sort the duplicates out of your result.

var results = rootFields.ToList().Select(field => field.Group).Distinct();

foreach (var_group in results)
{
    fieldsList.Add(new SelectListItem { Text = _group });
}
Mark
  • 3,273
  • 2
  • 36
  • 54
  • foreach cannot convert string to class ...Field. If i use var instead of field in the foreach I cannot receive the .Group definition. – Robin Nov 03 '14 at 12:20
  • @RangeRover see my edit, the results only are a collection of string's which are representing the group names – Mark Nov 03 '14 at 12:24