Copied to clipboard

Flag this post as spam?

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


  • Ben Palmer 180 posts 866 karma points c-trib
    Mar 31, 2017 @ 10:01
    Ben Palmer
    0

    Creating a Custom Data Type Programatically

    Hi,

    I'm currently in the process of creating a plugin and want to create document types and data types programatically. I've figured out the document type part, or I'm at least on the right track but have run into an issue created a new data type.

    I've figured out how to create a new data type based on existing propertyEditors - I've created a data type based on the repeatable strings property editor but can't quite figure out how to set configuration values. Here's my code:

    var dataTypeContainer = Services.DataTypeService.CreateContainer(-1, "Umbraco Rss Feeds");
    
    if (dataTypeContainer.Success)
    {
        var dataType = new DataTypeDefinition(dataTypeContainer.Result.Entity.Id, "Umbraco.MultipleTextstring");
        dataType.Name = "Allowed Content Types";
        Services.DataTypeService.Save(dataType);
    }
    

    So first I'm creating a container/folder to hold my new data type which works fine. Then if that's successful, I go on to create a new data type based on the Umbraco.MultipleTextstring property editor. This all works but I want to further customise this new data type by setting the Maximum and Minimum configuration values.

    I've been through the properties available to me through code completion but can't find anything to help me set the values.

    I've also started to question if it's even possible to do it this way. I found code in the umbraco repository that I'm wondering if I need to hook into/extend but still can't see anything for the configuration.

    Thanks in advance for any help,

    Ben

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 11, 2018 @ 16:43
    Michaël Vanbrabandt
    0

    Hi Ben,

    if I am correct I think the DataTypeDefinition object has a property called AdditionalData which is a Dictionary<string, object> type.

    There you can access the Minimum and Maximum configuration properties.

    Hope this helps!

    /Michaël

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 12, 2018 @ 15:50
    Michaël Vanbrabandt
    0

    Hi Ben,

    did you had the chance to solve your issue?

    Could you report back and share this on the community?

    /Michaël

  • Ben Palmer 180 posts 866 karma points c-trib
    Mar 12, 2018 @ 16:36
    Ben Palmer
    102

    Actually I did!

    You need to save the data type along with prevalues. For example, to set a single image picker, I've used the following code:

    Dictionary<string, PreValue> preValues = new Dictionary<string, PreValue>();
    
    preValues.Add("onlyImages", new PreValue("1"));
    preValues.Add("disableFolderSelect", new PreValue("1"));
    
    DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(containerId, "Umbraco.MediaPicker2");
    
    dataTypeDefinition.Name = "Single Image Picker";
    
    _dataTypeService.SaveDataTypeAndPreValues(dataTypeDefinition, preValues);
    

    Hope it helps anyone else who might stumble upon the same issue.

Please Sign in or register to post replies

Write your reply to:

Draft