In part 1, I asked a question on how to create a DataType in code in Umbraco 14+ (
https://discord.com/channels/869656431308189746/1327303970724777984) and fortunately I found the solution for that. However, now that I've been working more with the code, the experience is terrible. This post is two fold: inform people who want to do this as well and also decide if I should create a bug report or not. In this example, the result is the data type in the attachment; a slider on a scale of 1 to 10.
First we need to get the property editor to use, which is the slider:
csharp
//propertyEditorCollection is an injected PropertyEditorCollection
_ = propertyEditorCollection.TryGet("Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.Slider", out var propertyEditor);
Next we need to create the configuration for our new DataType:
csharp
var dataTypeConfiguration = new SliderConfiguration
{
EnableRange = false,
MinimumValue = 1,
MaximumValue = 10,
};
//configurationEditorJsonSerializer is an injected IConfigurationEditorJsonSerializer
var configurationDictionary = propertyEditor?.GetConfigurationEditor().FromConfigurationObject(_configuration, configurationEditorJsonSerializer);
And finally create the data type:
csharp
//configurationEditorJsonSerializer is an injected IConfigurationEditorJsonSerializer
var newType = new DataType(propertyEditor, configurationEditorJsonSerializer)
{
DatabaseType = ValueStorageType.Ntext,
ConfigurationData = configurationDictionary,
CreateDate = DateTime.Now,
CreatorId = Umbraco.Cms.Core.Constants.Security.SuperUserId, //Deprecation warning on SuperUserId!
Name = "Example slider"
};
var result = await dataTypeService.CreateAsync(newType, Umbraco.Cms.Core.Constants.Security.SuperUserKey);
Nice and makes sense! Except that this doesn't work and there are multiple issues with this. (See next post)
https://cdn.discordapp.com/attachments/1331176660518699062/1331176661106032701/image.png?ex=6790aa60&is=678f58e0&hm=71840613f6c02ef1e3d9eef31ae58b581ed658cec6905db712409574bd3ee59e&