This utility generates strongly-typed (POCO) models based on Content Types in a Kentico Kontent project. You can choose one of the following:
- Generate models compatible with the Kontent Delivery SDK for .NET
- Generate models compatible with the Kontent Management SDK for .NET.
How to use for Delivery SDK
To fully understand all benefits of this approach, please read the documentation.
The recommended way of obtaining this tool is installing it as a .NET Core 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
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
Then you can start using the KontentModelGenerator
command in the command-line right away.
dotnet tool run KontentModelGenerator --projectid "<projectid>" [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--withtypeprovider <True|False>] [--structuredmodel <True|False>] [--filenamesuffix "<suffix>"]
Latest release: Download
Since the app is self-contained, it's an ideal option for machines without .NET Core or .NET Core SDK installed.
Usage:
KontentModelGenerator.exe --projectid "<projectid>" [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--withtypeprovider <True|False>] [--structuredmodel <True|False>] [--filenamesuffix "<suffix>"]
- Clone the repository
- Navigate to the
kontent-generators-net\src\KontentModelGenerator
folder - Run
dotnet build -r <RID>
to build the app - Run
dotnet publish -c release -r <RID>
to publish the app
dotnet run --projectid "<projectid>" [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--withtypeprovider <True|False>] [--structuredmodel <True|False>] [--filenamesuffix "<suffix>"]
See the list of all RIDs.
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 | false |
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 |
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.
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.
There are two ways of configuring advanced Delivery SDK options (such as secure API access, preview API access, and others):
-
Command-line arguments
--DeliveryOptions:UseSecureAccess true
(syntax) -
appSettings.json
- suitable for the standalone app release
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; }
}
}
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 [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--filenamesuffix "<suffix>"]
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.
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; }
}
}
Check out the contributing page to see the best places to file issues, start discussions and begin contributing.
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!