Copied to clipboard

Flag this post as spam?

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


  • Thobias Michel 18 posts 99 karma points
    Feb 14, 2020 @ 17:05
    Thobias Michel
    0

    Create DataType programmitcally Umbraco V8.5.2

    Hey@all,

    is there an easier way to create a new property like nested content programmatically?

    I tried a little bit with the DataTypeService, but i think there is a way, easier than creating a lot of nested objects.

    I think a little example would be very helpful.

        var dataType = new DataType(new DataEditor(new DebugDiagnosticsLogger()));
    var existingDataType = new DataType(dataType.Editor);
    
    existingDataType.Level = customDataType.Level;
    existingDataType.Path = customDataType.Path;
    existingDataType.CreateDate = customDataType.CreateDate;
    existingDataType.CreatorId = customDataType.CreatorId;
    existingDataType.DeleteDate = customDataType.DeleteDate;
    existingDataType.ParentId = customDataType.ParentId;
    existingDataType.SortOrder = customDataType.SortOrder;
    existingDataType.UpdateDate = customDataType.UpdateDate;
    existingDataType.Key = customDataType.Key;
    existingDataType.Name = customDataType.Name;
    
    if(customDataType.EditorAlias.Equals("Umbraco.NestedContent"))
        existingDataType.Editor = new NestedContentPropertyEditor(null, new Lazy<PropertyEditorCollection>());
    
    if(customDataType.EditorAlias.Equals("Umbraco.NestedContent"))
          existingDataType.Configuration = new Umbraco.Web.PropertyEditors.NestedContentConfiguration()
          {
                ConfirmDeletes = customDataType.Configuration.ConfirmDeletes ?? false,
                ContentTypes = customDataType.Configuration.ContentTypes.Select(c => new                   Umbraco.Web.PropertyEditors.NestedContentConfiguration.ContentType()
          {
              Alias = c.ncAlias,
              Template = c.nameTemplate,
              TabAlias = c.ncTabAlias
          }).ToArray(),
          HideLabel = customDataType.Configuration.HideLabel ?? false,
          MaxItems = customDataType.Configuration.MaxItems,
          MinItems = customDataType.Configuration.MinItems,
          ShowIcons = customDataType.Configuration.ShowIcons ?? true
    };
    
  • Thobias Michel 18 posts 99 karma points
    Feb 17, 2020 @ 13:12
    Thobias Michel
    0

    Solution

    var editor = propertyEditors.FirstOrDefault<IDataEditor>((IDataEditor x) =>
                    x.Alias == customDataType.EditorAlias);
    
                var existingDataType = services.DataTypeService.GetAll().FirstOrDefault(d => d.Key.Equals(customDataType.Key));
    
                if (existingDataType == null)
                {
                    existingDataType = new DataType(editor)
                    {
                        Name = customDataType.Name
                    };
                }
    
Please Sign in or register to post replies

Write your reply to:

Draft