Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Peter Aderhold 30 posts 204 karma points
    Jan 05, 2020 @ 13:17
    Peter Aderhold
    0

    How to add PreValues programmatically into DropdownList in v8?

    Hi folks,

    I found some examples for v7 and earlier, but not for v8. Anyone knows how to do this?

    I have so far:

    public static void SetDataTypePreValues(int id, List<string> values)
        {
            var dataTypeService = Current.Services.DataTypeService;
            var dataType = dataTypeService.GetDataType(id); // this is the dropdownlist
            // and now???
    
        }
    

    Thank you for your help.

    Peter

  • Peter Aderhold 30 posts 204 karma points
    Jan 05, 2020 @ 14:22
    Peter Aderhold
    101

    What I did for now is:

    var valueList = (ValueListConfiguration)dataType.Configuration;
            var lastId = valueList.Items.LastOrDefault()?.Id;
            var newValueList = new List<ValueListConfiguration.ValueListItem>();
            var idCounter = lastId ?? 0;
            foreach (var val in values)
            {
                newValueList.Add(new ValueListConfiguration.ValueListItem
                {
                    Id = idCounter,
                    Value = val
                });
    
                idCounter++;
            }
            ((ValueListConfiguration)dataType.Configuration).Items.AddRange(newValueList);
            dataTypeService.Save(dataType);
    

    I don't know if this is the best way to do it, but at least it works.

  • Alasdair 4 posts 74 karma points
    May 29, 2021 @ 18:47
    Alasdair
    1

    Thanks for sharing Peter!

    By combining your value adding with this answer: https://our.umbraco.com/forum/extending-umbraco-and-using-the-api/97597-creating-custom-data-type#comment-308044

    I was able to create a composer to migrate a collection of select lists from a bespoke CMS. Hopefully this helps someone else.

    public class CreateDataTypeComponent : IComponent
    {
        private readonly ILogger _logger;
        protected IDataTypeService dataTypeService = Current.Services.DataTypeService;
        public static DbMigrate db = new DbMigrate();
    
        public CreateDataTypeComponent(ILogger logger)
        {
            _logger = logger;
        }
    
        public void Initialize()
        {
            var select_list = db.policy_select_list_types;
            foreach (var list in select_list)
            {
                string dataTypeName = "Dropdown - " + list.name;
                const string editorAlias = "Umbraco.DropDown.Flexible";
    
                var exists = dataTypeService.GetDataType(dataTypeName) != null;
                if (!exists)
                {
                    var created = Current.PropertyEditors.TryGet(editorAlias, out IDataEditor editor);
                    if (created)
                    {
                        DataType newDataType = new DataType(editor)
                        {
                            Name = dataTypeName
                        };
    
                        var values = db.policy_select_list_values.Where(x => x.select_list_id == list.id);
                        var newValueList = new List<ValueListConfiguration.ValueListItem>();
                        var idCounter = 0;
                        foreach (var val in values)
                        {
                            if (val.display_text != "")
                            {
                                newValueList.Add(new ValueListConfiguration.ValueListItem
                                {
                                    Id = idCounter,
                                    Value = val.display_text
                                });
    
                                idCounter++;
                            }
                        }
                        ((ValueListConfiguration)newDataType.Configuration).Items.AddRange(newValueList);
    
                        dataTypeService.Save(newDataType);
                        int newDataTypeId = newDataType.Id;
                        _logger.Info<CreateDataTypeComponent>(newDataType.Name + " : " + newDataType.Id);
    
                    }
                }
            }
        }
        public void Terminate()
        {
        }
    }
    
  • Khupi 7 posts 117 karma points
    Sep 20, 2022 @ 14:26
    Khupi
    0

    Works like a charm, I wouldn't know how to include ValueListConfiguration property. Thanks buddy

Please Sign in or register to post replies

Write your reply to:

Draft