Using Umbraco.ModelsBuilder.API To Create Strongly Typed Models Within Umbraco

In this tutorial, you will learn how to create strongly-typed models to use with your document types. (If you need a little more information why this is a good design approach, then I suggest you read this first).

Prerequisites For Generating Models

First, you will need to install the ModelsBuilder.API package into your solution.  You can install using the below command in your package console:

Install-Package Umbraco.ModelsBuilder.Api

Next, you will need a model builder visual studio extension to call the API's and generate the classes.  The visual studio extension is available from here Umbraco Models Builder Custom Tool.  Or you can install it within Visual Studio -> Tools -> Extensions and features -> Online.

Configuring Your Solution

In your web.config you will need the following app settings:

<add key="Umbraco.ModelsBuilder.EnableApi" value="true" />
<add key="Umbraco.ModelsBuilder.Enable" value="true" />
<add key="Umbraco.ModelsBuilder.ModelsMode" value="Nothing" />

Next, we need to configure the ModelBuilder custom tool.  Within Visual Studio, go to Tools -> Options -> Umbraco

On this screen, you will need to add in the details that will allow the tool to access your Umbraco website, which is your local website URL and your Umbraco username and password.

Generating Your Models

First, create an empty class library in your solution.  In there create an empty class and call it 'builder.cs'.  Right-click on it and select 'properties'

In the custom tool field set it to 'UmbracoModelsBuilder'

Models should be auto-magically created, if they don't, you can right-click on the builder class and click 'run custom tool'.  This should generate the models for you.  Compile your solution and you should be good to go... enjoy! 

NOTE:  If you struggle to find where the files are generated, expand the builder.  All the generated classes get created as children of it!

 

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top