Copied to clipboard

Flag this post as spam?

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


  • Ryios 122 posts 263 karma points
    May 15, 2014 @ 16:11
    Ryios
    0

    Register DataType/PropertyEditor Programatically

    I have created a PropertyEditor with the Angular JS model and that works fine.  However, it has to be created in the Umbraco Back Office (registered) and this is against the desired Flow I need out of a package I am working on.

    I need to register the DataType programatically and despite my searches I have been unable to figure out how to do that.  I have found a ton of info on creating DocumentTypes programatically, but not registering DataTypes(aka Property Editor's).

    Has anyone found a way to do this?

    I've resorted to digging through Reflector, but am still looking so through I would make a thread on the topic.

  • Ryios 122 posts 263 karma points
    May 15, 2014 @ 18:21
    Ryios
    0

    [deleted]

  • Ryios 122 posts 263 karma points
    May 22, 2014 @ 20:34
    Ryios
    100

    I came back to this issue and managed to figure it out "Thanks Reflector!"

    I am Inheriting from ApplicationEventHandler to add the logic to Register DataTypes in the ApplicationStarted Event,

        public class StartupHandler : ApplicationEventHandler
    {
        private Dictionary<string, IDataTypeDefinition> keyedDataTypes = new Dictionary<string, IDataTypeDefinition>();
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            base.ApplicationStarted(umbracoApplication, applicationContext);                       
            //Get All DataTypes
            List<IDataTypeDefinition> dataTypes = applicationContext.Services.DataTypeService.GetAllDataTypeDefinitions().ToList();
            dataTypes.ForEach(p =>
            {
                if (!this.keyedDataTypes.ContainsKey(p.Name))
                    this.keyedDataTypes.Add(p.Name, p);
            });
    
            if (!this.keyedDataTypes.ContainsKey("GMail Calendar"))
            {
                DataTypeDefinition dtfGmailCalendar = new DataTypeDefinition(-1, null) { Name = "GMail Calendar", PropertyEditorAlias = "PluginName.DataTypes.GMailCalendar" };
                applicationContext.Services.DataTypeService.Save(dtfGmailCalendar, 0);
            }            
        }
    }
    

    Really it's as simple as Creating a DataTypeDefinition Instance and calling DataTypeService.Save on it passing in a UserID of 0.

    Because I used -1 for the ID of the DataType the Umrbaco API creates one automatically. In this case it created it with an ID of 1050 with the last Out of the box ID being 1045, (I've created and deleted mine multiple times).

    To get a good visual on this happening, I opened up MySql Workbench and just ran a select query on cmsdatatype, I can see it delete when I delete the datatype in the UI and I can see it create itself when the Site Starts up.

  • Ryios 122 posts 263 karma points
    May 22, 2014 @ 20:47
    Ryios
    0

    Follow up,

    The confusing part is the DataType's name. You can create multiple datatypes using the same Property Editor and give them different names. I figure this is because DataTypes can be associated with PreValues used as configuration for the PropertyEditor.

    However, the Name of the datatype is not stored in the cmsdatatype table. It is stored in the umbraconode table. So I've deduced that everything in the tree's in the backoffice is stored as an umbraconode.

    The confusing part is you could end up with two datatype entries in cmsdatatype that only differ by ID and the prevalues associated with them in cmsdatatypeprevvalues. As to which one goes to which Name, that is pulled from umbraconode via the ID field.

    In short to get the name associated with the datatype, you join cmsdatatype on umbraconode by the nodeID = ID fields.

    Of course, in use, you just use the DataTypeService, it does all this under the hood. But if your like me and you like to visually look at the sql to better understand what's going on, this is the relation.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies