0
votes

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?

1

1 Answers

0
votes

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 });
}