Skip to content
master
Go to file
Code

README.md

Build status NuGet Stack Overflow

Kentico Kontent model generator utility for .NET

This utility generates strongly-typed (POCO) models based on Content Types in a Kentico Kontent project. You can choose one of the following:

How to use for Delivery SDK

To fully understand all benefits of this approach, please read the documentation.

.NET Tool

The recommended way of obtaining this tool is installing it as a .NET Tool. You can install it as a global tool or per project as a local tool.

Global Tool

  • dotnet tool install -g Kentico.Kontent.ModelGenerator
  • KontentModelGenerator --projectid "<projectid>" [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--withtypeprovider <True|False>] [--structuredmodel <True|False>] [--filenamesuffix "<suffix>"]

Local Tool

  • dotnet new tool-manifest to initialize the tools manifest (if you haven't done that already)
  • dotnet tool install Kentico.Kontent.ModelGenerator (to install the latest version
  • dotnet tool run KontentModelGenerator --projectid "<projectid>" [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--withtypeprovider <True|False>] [--structuredmodel <True|False>] [--filenamesuffix "<suffix>"]

Standalone apps for Windows 🗔, Linux 🐧, macOS 🍎

Self-contained apps are an ideal choice for machines without any version of .NET installed.

Latest release: Download

  • KontentModelGenerator --projectid "<projectid>" [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--withtypeprovider <True|False>] [--structuredmodel <True|False>] [--filenamesuffix "<suffix>"]

To learn how to generate executables for your favorite target platform, follow the steps in the wiki.

Parameters

Short key Long key Required Default value Description
-p --projectid True null A GUID that can be found in Kentico Kontent -> API keys -> Project ID
-n --namespace False KenticoKontentModels A name of the C# namespace
-o --outputdir False \. An output folder path
-g --generatepartials False true Generates partial classes for customization. Partial classes are the best practice for customization so the recommended value is true.
-t --withtypeprovider False true Indicates whether the CustomTypeProvider class should be generated (see Customizing the strong-type binding logic for more info)
-s --structuredmodel False false Generates IRichTextContent instead of string for rich-text elements. This enables utilizing structured rich-text rendering
-f --filenamesuffix False null Adds a suffix to generated filenames (e.g., News.cs becomes News.Generated.cs)
-b --baseclass False null If provided, a base class type will be created and all generated classes will derive from that base class via partial extender classes

CLI Syntax

Short keys such as -s true are interchangable with the long keys --structuredmodel true. Other possible syntax is -s=true or --structuredmodel=true. Parameter values are case-insensitive, so you can use both -s=true and -s=True. To see all aspects of the syntax, see the MS docs.

Config file

These parameters can also be set via the appSettings.json file located in the same directory as the executable file. Command-line parameters always take precedence.

Advanced configuration (Preview API, Secure API)

There are two ways of configuring advanced Delivery SDK options (such as secure API access, preview API access, and others):

  1. Command-line arguments --DeliveryOptions:UseSecureAccess true (syntax)

  2. appSettings.json - suitable for the standalone app release

Example output

using System;
using System.Collections.Generic;
using Kentico.Kontent.Delivery.Abstractions;

namespace KenticoKontentModels
{
    public partial class CompleteContentType
    {
        public string Text { get; set; }
        public string RichText { get; set; }
        public decimal? Number { get; set; }
        public IEnumerable<MultipleChoiceOption> MultipleChoice { get; set; }
        public DateTime? DateTime { get; set; }
        public IEnumerable<Asset> Asset { get; set; }
        public IEnumerable<object> ModularContent { get; set; }
        public IEnumerable<TaxonomyTerm> Taxonomy { get; set; }
        public string UrlSlug { get; set; }
        public string CustomElement { get; set; }
        public ContentItemSystemAttributes System { get; set; }
    }
}

Customizing models - Handling content element constraints

Currently, the generator is built on top of the Delivery API which doesn't provide information about content element constraints such as "Allowed Content Types" or "Limit number of items". In case you want your models to be more specific, this is the best practice on how to extend them:

Model.Generated.cs

public partial class Home
{
    public IEnumerable<object> LinkedContentItems { get; set; }
}

Model.cs

public partial class Home
{
    // Allowed Content Types == "Article"
    public IEnumerable<Article> Articles => LinkedContentItems.OfType<Article>();
	
    // Allowed Content Types == "Article" && Limit number of items == 1	
    public Article Article => LinkedContentItems.OfType<Article>().FirstOrDefault();
}

How to use for Management SDK

Usage:

KontentModelGenerator.exe --projectid "<projectid>" --contentmanagementapi true [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--filenamesuffix "<suffix>"]

Parameters

Short key Long key Required Default value Description
-p --projectid True null A GUID that can be found in Kentico Kontent -> API keys -> Project ID
-c --contentmanagementapi True false Indicates that models should be generated for Content Management SDK
-n --namespace False KenticoKontentModels A name of the C# namespace
-o --outputdir False \. An output folder path
-f --filenamesuffix False null Adds a suffix to generated filenames (e.g., News.cs becomes News.Generated.cs)
-b --baseclass False null If provided, a base class type will be created and all generated classes will derive from that base class via partial extender classes

These parameters can also be set via the appSettings.json file located in the same directory as the executable file. Command-line parameters always take precedence.

Example output

using System;
using System.Collections.Generic;
using Kentico.Kontent.Management.Models.Assets;
using Kentico.Kontent.Management.Models.Items;
using Newtonsoft.Json;

namespace KenticoKontentModels
{
    public partial class CompleteContentType
    {
        public string Text { get; set; }
        public string RichText { get; set; }
        public decimal? Number { get; set; }
        public IEnumerable<MultipleChoiceOptionIdentifier> MultipleChoice { get; set; }
        public DateTime? DateTime { get; set; }
        public IEnumerable<AssetIdentifier> Asset { get; set; }
        public IEnumerable<ContentItemIdentifier> ModularContent { get; set; }
        public IEnumerable<TaxonomyTermIdentifier> Taxonomy { get; set; }
        public string UrlSlug { get; set; }
	public string CustomElement { get; set; }
    }
}

Feedback & Contributing

Check out the contributing page to see the best places to file issues, start discussions and begin contributing.

Wall of Fame

We would like to express our thanks to the following people who contributed and made the project possible:

Would you like to become a hero too? Pick an issue and send us a pull request!

Analytics

You can’t perform that action at this time.