Create DataType in code part 2 - very bad experien...
# help-with-umbraco
l
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:
Copy code
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:
Copy code
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:
Copy code
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&
The above code will create the new data type, but it will have no property editor set. To fix this, you NEED to set the EditorUiAlias property:
Copy code
csharp
var newType = new DataType(propertyEditor, configurationEditorJsonSerializer)
{
  DatabaseType = databaseType,
  ConfigurationData = dictionary,
  CreateDate = DateTime.Now,
  CreatorId = -1,
  Name = dataTypeName,
  EditorUiAlias = "Umb.PropertyEditorUi.Slider"
};
This property is nullable and I would assume that because we give the propertyEditor in the constructor that we wouldn't need to set it, but we do. There doesn't seem to be a list of constants in Umbraco either for this, too bad. And a sidenote: CreatorId is still an integer, so if we use Umbraco.Cms.Core.Constants.Security.SuperUserId, we get all kinds of deprecation warnings... So now we have a data type with a correctly set Property editor, but we defined the configuration of our slider on a scale of 1 to 10. This doesn't work either, all values (minumum value, maximum value, step etc) remain 0. It turns out that the SliderConfiguration model simply is not up to date. Looking at the properties that are set when saving a slider data type, it's clear that not all properties are present in that model as it has only three properties. Also the configurator (propertyEditor?.GetConfigurationEditor()) has three properties. So when I create the property dictionary myself, I can finally get it to work:
Copy code
csharp
var dictionary = new Dictionary<string, object>()
{
  {"enableRange", false},
  {"minimumValue", 1},
  {"maximumValue", 10},
  {"minVal", 1},
  {"maxVal", 10},
  {"initVal1", 5},
  {"step", 1}
};

var newType = new DataType(propertyEditor, configurationEditorJsonSerializer)
{
  DatabaseType = databaseType,
  ConfigurationData = dictionary,
  CreateDate = DateTime.Now,
  CreatorId = -1,
  Name = dataTypeName,
  Editor = propertyEditor,
  EditorUiAlias = "Umb.PropertyEditorUi.Slider"
};
So in the end: I can't use the strong typed settings model, have to use aliasses that I just need to look up and have no constants, use an int UserId, have to look at the backoffice call to create the correct dictionary. I'd say that's pretty lousy developer experience, not to mention the time it took me to figure this out...
p
Would be great to put this into either a blog or add it to the Umbraco documentation (if it isn't already somewhere). I do think it would be good to create an issue because of the CreatorId& the EditorUiAlias being nullable. Sounds like that hasn't been moved over correctly
3 Views