Copied to clipboard

Flag this post as spam?

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


  • Amar Trebinjac 3 posts 93 karma points
    Apr 29, 2019 @ 08:42
    Amar Trebinjac
    0

    Creating a Drop Down programatically

    Hi, I'm trying to create a drop down programatically, but I'm having some issues. I've been googling around quite a bit, and found something that should work, but for some reason doesn't. Here is what I have currently:

        Dictionary<string, PreValue> dictionary = new Dictionary<string, PreValue>();
        dictionary.Add("firstOption", new PreValue("1"));
        dictionary.Add("SecondOption", new PreValue("2"));
    
        var dataType = new DataTypeDefinition(-1, "Umbraco.DropDown.Flexible");
        dataType.Name = "MyDropDown";
        Services.Save(dataType);
        Services.SaveDataTypeAndPreValues(dataType, dictionary);
    

    This works with other data types that require prevalues, such as for example Radio List, but for the drop down it doesn't for some reason, and I just get a DB mapping error.

  • Amar Trebinjac 3 posts 93 karma points
    Apr 29, 2019 @ 10:43
    Amar Trebinjac
    100

    So after some more Googling I finally found the answer. The above solution is almost complete, but it is missing a value which was not quite obvious. You need to specify if it should be a multiple picker or not like so:

    Dictionary<string, PreValue> dictionary = new Dictionary<string, PreValue>();
    dictionary.Add("firstOption", new PreValue("1"));
    dictionary.Add("SecondOption", new PreValue("2"));
    dictionary.Add("multiple", new PreValue("0"));
    
    
    var dataType = new DataTypeDefinition(-1, "Umbraco.DropDown.Flexible");
    dataType.Name = "MyDropDown";
    Services.Save(dataType);
    Services.SaveDataTypeAndPreValues(dataType, dictionary);
    

    Not including the "multiple" value will still create the drop down, but it will be corrupted and unusable. Hope this helps anyone stuck on this in the future.

    For reference, I found the solution in this file: https://github.com/umbraco/OurUmbraco/blob/master/OurUmbraco/Our/MigrationsHandler.cs

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies