Umbraco Modelsbuilder extend partial class

Relates to Umbraco 8.02

Since Umbraco released the models builder, they have speeded up the building of websites and as the code generated creates the document type and properties for us, this allows us to have intellisense in Visual Studio allowing for strongly types models.

Since the models builder has all it classes auto-generated in visual studio, this means that we cannot alter the properties within the class as they will be overridden on the next update.

Example Partials class

The code shown is an example of what is generated by the models builder taken from a demo website I created for this blog

//------------------------------------------------------------------------------
// <auto-generated>
//   This code was generated by a tool.
//
//    Umbraco.ModelsBuilder v8.0.4
//
//   Changes to this file will be lost if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web;
using Umbraco.ModelsBuilder;
using Umbraco.ModelsBuilder.Umbraco;

namespace Web.ContentHelper.UmbracoModelBuilder
{
	/// <summary>Image And Text</summary>
	[PublishedModel("imageAndText")]
	public partial class ImageAndText : PublishedElementModel
	{
		// helpers
#pragma warning disable 0109 // new is redundant
		[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.0.4")]
		public new const string ModelTypeAlias = "imageAndText";
		[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.0.4")]
		public new const PublishedItemType ModelItemType = PublishedItemType.Content;
		[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.0.4")]
		public new static PublishedContentType GetModelContentType()
			=> PublishedModelUtility.GetModelContentType(ModelItemType, ModelTypeAlias);
		[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.0.4")]
		public static PublishedPropertyType GetModelPropertyType<TValue>(Expression<Func<ImageAndText, TValue>> selector)
			=> PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector);
#pragma warning restore 0109

		// ctor
		public ImageAndText(IPublishedElement content)
			: base(content)
		{ }

		// properties

		///<summary>
		/// Image Width
		///</summary>
		[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.0.4")]
		[ImplementPropertyType("imageWidth")]
		public string ImageWidth => this.Value<string>("imageWidth");

	}
}

As you can see in the example code above, it warns you about modify the code.

Changes to this file will be lost if the code is regenerated.

So how do we add our own code to the partial class?

Fortunately, the class is partial, this means that we can create our own class within the same namespace and also make that partial.

Therefore say we do not add width to the image in the CMS, we can now create a property called ImageDefaultWidth and add a default value to the image width. Now, all we need to do is instead of displaying imageWidth, we can display the value of ImageDefaultValue.

Replace imageWidth with DefaultImageWidth
namespace Web.ContentHelper.UmbracoModelBuilder
{
    public partial class ImageAndText
    {
        public int ImageDefaultWidth => this.Value<int>("imageWidth", defaultValue: 320);
    }
}

Although this was a simple example, I hope it shows you how easy it is to extend partial classes when working with visual studio and the Umbraco models builder.

If you found this post helpful, please leave a comment below.

Blog Form

 Please complete the required fields (*required)

 *
*