Page type code generation with Xperience by Kentico
15/03/2022How does code generation work for generating classes for your page types?
Working with strongly typed objects for page types has always been a preference for Kentico Xperience developers. Nobody wants to work with a TreeNode directly, right? You want a class that inherits from TreeNode, contains all the page type fields, and uses the correct data types throughout.
In previous versions of Kentico Xperience, we were provided with a few different ways of generating strongly typed classes. In all scenarios, you had to authenticate with the administration portal and navigate to the Page Types module. From there you could either copy and paste the individual generated classes or click a button to save them to a folder within the project.
Xperience by Kentico, being the next generation of Kentico Xperience, wanted to improve and build upon this key piece of developer functionality. Since Xperience by Kentico is entirely rewritten in .NET Core, it is more appropriate that the new code generation tool sits outside of the administration portal and is access through a .NET CLI command.
The new .NET CLI command is so simple to use and was extremely useful when migrating my blog website to Xperience by Kentico. If I created or edited a page type, I just opened PowerShell and reran the same command:
dotnet run --no-build -- --kxp-codegen --type "All" --location "../Goldfinch.Core/{type}/{dataClassNamespace}/{name}/"
The example above would generate all page type classes, without rebuilding the project first. The last parameter for location allowed me to customize where the generated class files were placed in my project. The CLI command comes with other options for including or excluding objects based on patterns, skipping the confirmation warning message, and customizing the namespaces of the files being generated.
If you need any further help getting started with the code generation tool, then I would highly recommend checking the documentation page. It covers all aspects of the code generation tool and provides explanations and examples for each of the parameters available.
You might also be interested in...
Keeping your Xperience by Kentico project up-to-date using Dependabot
25/01/2024Secure your Xperience by Kentico website with Dependabot - an automated package scanner for timely updates. Learn how I integrated it into Azure DevOps for my blog site.
Setting up Azure Key Vault in Kentico Xperience 13
23/10/2023Azure Key Vault is useful for securing sensitive data for both Kentico Xperience 13 presentation and administration websites, but how do you implement it? Let's take a look!
Creating a Custom Module in Xperience by Kentico
04/08/2023Kentico has always excelled at allowing developers to extend the admin site. However, with Xperience by Kentico, there are some notable differences. Let's walk through a step-by-step process for creating custom modules in Xperience by Kentico.