Get possible values in a Profile enumeration field

Topics: Writing modules
Apr 19, 2013 at 11:36 AM
Hi, I'm triying to get possibles enumeration values in a profile enumeration field. I'm check in debugger and I think this is the call:
var enumerationElements = userProfile.EnumerationField.PartFieldDefinition.Settings[3].Value;
Debbuger throw this error:
La mejor coincidencia de método sobrecargado para 'System.Collections.Generic.Dictionary<string,string>.this[string]' tiene algunos argumentos no válidos

How I can get enumeration values?

Thanks in advanced.
Coordinator
Apr 22, 2013 at 5:32 AM
You can infer it from what Enumeration.Edit.cshtml is doing:
    var settings = Model.PartFieldDefinition.Settings.GetModel<EnumerationFieldSettings>();
    string[] options = (!String.IsNullOrWhiteSpace(settings.Options)) ? settings.Options.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None) : new string[] { T("Select an option").ToString() };
If you replace the first line with:
var settings = ((SettingsDictionary)userProfile.EnumerationField.PartFieldDefinition.Settings).GetModel<EnumerationFieldSettings>();
you should be able to get the options.
Apr 23, 2013 at 4:42 PM
Thank you! I see you use view to find these values. I need find it from action in a controller, is possible?

Thanks in advance
Apr 23, 2013 at 6:19 PM
I got obtain all possibles enumeration values with this code:
dynamic userProfiles = _contentManager.Query<UserPart, UserPartRecord>()
                .List()
                .Select(user => ((dynamic)user).ProfilePart)
                .Select(user => new {
                    user.Nombre, 
                    user.Apellidos
                });
foreach(dynamic usuario in userProfiles) {
                string enumFields= usuario.Grupo.PartFieldDefinition.Settings.GetModel<EnumerationFieldSettings>().Options;
            }
enumFields is a string with all possible values. I want another simpler way to get this data. I need obtain only first element of "userProfiles" to get enumeration metadata, but I try with userProfiles.ToList(), userProfile.First(), userProfile.Single() and userProfile[0], but not work none.
Coordinator
Apr 24, 2013 at 2:59 AM
There is no point in making userProfiles dynamic. Use var instead of dynamic and you'll have no trouble using First. You can also get the part field definition without going through an actual item...
Apr 24, 2013 at 12:34 PM
Edited Apr 24, 2013 at 12:35 PM
Thanks for help, I change some code and can get needed data more faster:
var miembrosRAW = _contentManager.Query<UserPart, UserPartRecord>().List().Select(user => ((dynamic)user).ProfilePart);

            EquipoViewModel equipo = new EquipoViewModel {
                Miembros = miembrosRAW.Where(user => !string.IsNullOrEmpty(user.Grupo.Value)).Select(user => new {
                    user.Name, 
                    user.DateOfBirth
                }),
                Grupos = miembrosRAW.FirstOrDefault().Grupo.PartFieldDefinition.Settings.GetModel<EnumerationFieldSettings>().Options.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
            };
I don't know if this is a perfect solution but it is more fast and beautiful than other solution.