Create Contentment DataList in Code

Hello, I’m currently trying to create a Contentment DataType within my application.

The following code creates the DataType:

if(dropdownDataType is null)
{
    var parentId = 1289;

    var dataTypeId = Guid.Parse("191b7c04-a8ee-4e20-bb7d-f49d27172926");
    var editor = propertyEditors.FirstOrDefault(x => x.Alias.Equals("Umbraco.Community.Contentment.DataList"));
      
    var dataType = new DataType(editor, editorJsonSerializer, parentId)
    {
        Name = "Dropdown",
        Key = dataTypeId
    };
              
    dataTypeService.Save(dataType);
}

But how can I set the DataSource and ListEditor in code?

The DataSource is going to be an enum.

Hi @sebmuel, it’s been a while since I’ve attempted to create a new data-type programmatically, but for Contentment’s Data List, it should be something like this…

var dataType = new DataType(editor, editorJsonSerializer, parentId)
{
    Name = "Dropdown",
    Key = dataTypeId,
    ConfigurationData = new Dictionary<string, object>
    {
        { "dataSource", new[] { new { key = "Umbraco.Community.Contentment.DataEditors.CountriesDataListSource, Umbraco.Community.Contentment", value = new { } } } },
        { "listEditor", new[] { new { key = "Umbraco.Community.Contentment.DataEditors.DropdownListDataListEditor, Umbraco.Community.Contentment", value = new { allowEmpty = false, showDescriptions = true, showIcons = true, } } } }
    }
};

For both the dataSource and listEditor values, the structure is { key: string, value: object }. The key is the full name of the class (including namespaces) and the assembly reference; and the value is an anonymous object of name/value pairs for the configuration of the class.

I hope this helps.

Cheers,
Lee.

Hey Lee,

thanks alot for your time. Im going to try that asap.

See you in Codegarden :wink:

1 Like