Post Pic

DataType Support in uSiteBuilder for Umbraco

When I first discovered uSiteBuilder for Umbraco I knew right away that it was a game changer in the world of Umbraco development (for me at least!). I always felt a little uncomfortable about having to leave the relative safety of my IDE (Visual Studio 2010) and source control (Mercurial) to work in the browser when creating document types, wiring up templates and datatypes. uSiteBuilder is great because it allows you to take a code-first approach to your development allowing you to develop all of your document types, document type properties and templates with strongly typed code and take advantage of the benefits of any source/version control system you work within.

I guess I have been working with uSiteBuilder since earlier this year, shortly after it was released and it is the first dependency I now add to any new Umbraco project. The only show stopper I’ve encountered rears it’s ugly head when working on a project with multiple developers and occurs when uSiteBuilder runs its synchronisation process and then throws an exception when it encounters a datatype on one of your document types that doesn’t exist in the target environment. The only real way around this is to comment out the offending document type property as recommended by Barry Fogarty, rebuild and deploy your solution, create the missing datatype and then uncomment, rebuild and deploy again which is a pain and slows down development.

Those kind people over at Vega IT Sourcing have released the source code for uSiteBuilder which is great for people like me who like to have a go at plugging the gaps myself. So I pulled down the source code and made a custom build in an attempt to support at least the default datatypes for Umbraco. I have tried to follow the same logic as the rest of the project so you don’t really need to think too much about it and can quickly familiarise yourself with  the process for creating your datatypes in code. Here is an example of a custom datatype based on the TinyMCE Richtexteditor datatype in Umbraco:

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StandardContentEditor.cs" company="Prolific Notion">
//   (c) Copyright 2011 Prolific Notion, http://prolificnotion.co.uk
// </copyright>
// <summary>
//   Defines the StandardContentEditor type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MyUmbracoProject.Web.DataTypes
{
    using umbraco.cms.businesslogic.datatype;

    using Vega.USiteBuilder;

    /// <summary>
    /// Defines the Standard Content Editor Datatype
    /// </summary>
    [DataType(
        UniqueId = "{8AB81EFB-35F5-4EF0-9300-04A3AC8A2D21}",
        Name = "Standard Content Editor",
        DatabaseDataType = DBTypes.Ntext,
        RenderControlGuid = "5e9b75ae-face-41c8-b47e-5f4b0fd82f83")]
    public class StandardContentEditor : DataTypeBase
    {
    }
}
}

Most of the attributes used here should be self explanatory however…

  1. UniqueId – A unique Guid for your datatype
  2. Name – The name of your datatype. This is displayed in the Umbraco UI.
  3. DatabaseDataType – This maps to the umbraco.cms.businesslogic.datatype.DBTypes Enum and defines the type of field to store data in the database.
  4. RenderControlGuid – Defines the Guid of the control you would like to render for this datatype. In the example above ‘5e9b75ae-face-41c8-b47e-5f4b0fd82f83’ is the Guid for the TinyMCE control.

There is one other attribute not in the example above called ‘RenderControlName‘ which I am in two minds whether to keep or not. Essentially its purpose would be to try and find the control by name and therefore grab it’s Guid if the Guid attribute was not provided, any thoughts or comments on this appreciated however I think it may be subject to clashes or getting the wrong control because names do not have to be unique.

The only other thing you need to remember is to inherit from DataTypeBase.

I am very happy to have been accepted as a contributor on the uSiteBuilder project this week and hope that I can contribute some of my work into the next release. For now, if you are interested in trying out the build for my initial proof of concept you can grab it on CodePlex, changset 84163. I welcome any feedback and comments.

Where do we go from here?

At this stage it’s really just a means of overcoming the exceptions I was previously encountering however I would ideally like to extend this to allow you to also configure your datatype settings in code so watch this space…!

Umbraco Certified Developer
SagePay Approved Partner
Creative Market