Create a reusable content type

Storing and displaying structured content is one of the fundamental parts of working with Xperience by Kentico. The structure of your content is determined by your company’s content model, an important part of which is reusable content.

Reusable content items are presentation-agnostic, structured pieces of content, stored in the Content hub. As the name suggests, they are reusable across multiple channels (web pages, emails and other applications).

In this guide, you will learn how to create a reusable content type in the Xperience administration interface and how to generate a partial class for it in your .NET solution, to access it and work with it programmatically.

First steps series - prerequisites

This guide is a part of the series about the first steps towards creating a site in Xperience by Kentico.

If you wish to follow along with the exercise, we recommend starting with Install a specific version of Xperience by Kentico guide.

Define the content type in the administration interface

Log into the administration interface of your project.

Create a new reusable content type with one field. It will contain a message to be displayed on landing pages, emails, and screens across multiple channels.

Naming convention for Content type fields

When creating the field names, we recommend prefixing them with the content type’s name (e.g. ArticleTitle, ArticleText for an Article content type). This naming convention will remove ambiguity in any scenarios where you have to work with multiple content types in code.

  1. From the dashboard, choose the Content types application under the Configuration heading.
  2. Click the New content type button, populate the form with the following properties, and save.
    • Display Name: Landing content

    • Namespace: TrainingGuides

    • Name: LandingContent

    • Icon: xp-market (type ‘market’ in the select list search bar)

    • Use for: select Reusable content from the dropdown

      Reusable content

      Xperience by Kentico allows you to create content that is reusable across multiple channels and channel types. We highly recommend the Reusable content property setting for any content type created with this intention.

      For example, in this case, the Landing content content type can be referenced and displayed on multiple separate websites or email channels, but it will only live in one place, the Content hub.

  3. Switch to the Fields tab.
  4. Add a new field to hold the message, with the following properties and save.
    • Field name: LandingContentMessage
    • Data type: Text
    • Size: 200
    • Required: False (un-checked)
    • Display in editing form: True (checked)
    • Field caption: Message
    • Form component: Text input

You have just created a reusable content type. If you now navigate to the Content Hub application (under the Content management category) and try to create a new content item, you will see Landing content as one of your content type options to choose from, alongside any additional reusable content types you may have created:

Add the content type to your solution

In order to access content types, created in the administration interface, in your .NET solution, you need to generate strongly typed classes for them. 

Such classes can be generated automatically using the dotnet run --kxp-code-gen .NET CLI command.

We recommend storing these generated classes in a separate project. With large projects, the separation of concerns into multiple layers helps ensure easier code maintenance in the future.

Create a new project for generated code

  1. Open your TrainingGuides solution with Visual Studio or your preferred source code editor.

  2. Add a new project, under the src folder using a Class Library template and name it TrainingGuides.Entities.

  3. Choose the .NET version that matches your existing project.

  4. Add a reference from the TrainingGuides.Web project to the TrainingGuides.Entities project. If you are using Visual Studio the steps are:

    1. Right-click on TrainingGuides.Web in the solution explorer.
    2. Choose Add → Project reference.
    3. Select the box for TrainingGuides.Entities, then click OK.
  5. Add the Kentico.Xperience.Core NuGet package to the TrainingGuides.Entities project, matching the installed Xperience version.

  6. Edit TrainingGuides.Entities.csproj to add the CMS.AssemblyDiscoverable attribute.

    The AssemblyDiscoverable attribute allows the Xperience API to recognize and register classes and implementations when the application starts.

TrainingGuides.Entities.csproj


...
  <ItemGroup>
    <AssemblyAttribute Include="CMS.AssemblyDiscoverableAttribute">
    </AssemblyAttribute>
  </ItemGroup>
...

Generate the class

Now let’s dive into generating the code for your new Landing content content type.

Open a command line in the TrainingGuides.Web folder and run the following command.

Alternatively, if you’ve completed the guide on automating recurring tasks, you can navigate to the Scripts folder and run the GenerateCodeFiles.ps1 script, which will generate code files for all system objects.

Generate code files


dotnet run --no-build -- --kxp-codegen --type "ReusableContentTypes" --location "../TrainingGuides.Entities/{type}/{name}" --include "TrainingGuides.LandingContent"

If you’d like to learn more about how to generate classes for different system objects created through the administration interface, this documentation page is our recommended reading for you. 

This video is another great resource that will walk you through some concrete examples.

After you successfully run the command above, you will see it created an LandingContent.generated.cs file for the TrainingGuides.LandingContent content type, located in the ~/ReusableContentTypes/LandingContent folder of the TrainingGuides.Entities project.

Thanks to the generated class, you can now retrieve and work with any future Landing content content items as described in the documentation.

What’s next?

The next guide in this series will show you how to utilize a reusable content type to create a page template and display content on a page.