Peter Nguyen Team : Web Development Tags : Technology Umbraco

Umbraco Code-First: two plugins to keep an eye out for

Peter Nguyen Team : Web Development Tags : Technology Umbraco

One of the pitfalls with Umbraco was that you had to generate the properties required for your documents through the UI. Some developers, such as myself, would much prefer creating properties inside view or data models in the backend as normal C# classes.

…and now you can!

After stumbling across a NuGet package named ‘Umbraco Inception’, I decided to integrate it with one of my projects that I was just starting.

Declaring document types is very simple – just decorate it with the UmbracoContentType attribute and you are ready to start.

I made up a HomePage model example which creates a ‘Home’ document type, and two tabs – one for Content and another for Media.

[UmbracoContentType("Home", "Home", null, createMatchingView: true, allowAtRoot: true, icon: BuiltInUmbracoContentTypeIcons.IconHome)]

    public class HomeModel : UmbracoGeneratedBase
        public HomeContentTab Content { get; set; }

        public HomeMediaTab Media { get; set; }

    public class HomeContentTab : TabBase
        [UmbracoProperty("Tagline", "tagline", BuiltInUmbracoDataTypes.Textbox)]
        public string Tagline { get; set; }

    public class HomeMediaTab : TabBase
        [UmbracoProperty("Logo Url", "logoUrl", BuiltInUmbracoDataTypes.MediaPicker, converterType: typeof(MediaIdConverter))]
        public string LogoUrl { get; set; }

That automatically created the appropriate document type, along with each Tab and appropriate properties.

How neat!

There are two catches (as far as I can tell at the moment) in using this approach:

  1. Document Types that are generated will not be visible in the Settings section in Umbraco.
  2. You will have to initialise each class individually when starting up Umbraco, by calling this:

Package is available here.


Another alternative, if you wish to still use the UI interface in Umbraco is Umbraco Code Gen.

It generates partial code classes and keeps it in-sync, so you can make changes to the partial code class and it will be synced with Umbraco and vice versa.

I haven’t tried it personally as I would prefer the more rigid approach of Umbraco Inception but this package does also have its merits.