Create a reusable content type

This page is part of a series you should follow sequentially from beginning to end. Go to the first step.

Storing and displaying structured content is one of the fundamental parts of working with any CMS. 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 headless channels).

In this step, we’ll create a reusable content type (a template for a reusable content item) in the Xperience administration interface. We’ll then generate a partial class for it in your .NET solution so that we can access and work with it programmatically.

Define the content type in the administration interface

Once you’ve signed in to the Xperience administration, we can get started.

Define the content type

From the dashboard, select the Content types application under the Configuration heading. Then click the New content type button and populate the form with the following properties:

  • Display Name: Slogan

  • Namespace: Kickstart

  • Name: Slogan

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

  • Use for: select Reusable content from the dropdown

    Screenshot of the content type creation page for the Slogan type

    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, you can reference and display the Slogan content type on multiple separate website, headless, and email channels, but it will only live in one place, the Content hub.

Add a field to the content type

After saving your new content type, switch to the Fields tab. There you can add a new field to hold the slogan, with the following properties:

  • Field name: SloganText
  • Data type: Text
  • Size: 500
  • Required: True (Enabled)
  • Display in editing form: True (Enabled)
  • Field caption: Slogan
  • Form component: Text input

Naming convention for Content type fields

We recommend prefixing field names with the content type’s name (e.g. ArticleTitle, ArticleText for an Article content type). This naming convention helps avoid problems when you have to work with multiple content types in code.

Check your progress

Now that we’ve gone through the steps to create a reusable content type, let’s make sure everything is working.

Navigate to the Content Hub application (under the Content management category) and try to create a new content item. You should see Slogan as one of the available content type options, alongside any additional reusable content types you may have created in your own experimentation:

Screenshot of the content hub application including the slogan type

Add the content type to your solution

The steps we’ve covered so far allow your editors to create new content items in the Xperience admin UI. However, to work with these content types as strongly-typed objects in code, we need the code generator.

You can automatically generate classes that correspond to your content types with the dotnet run --kxp-codegen .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

Create a project

Open your Kickstart solution with your preferred source code editor, and add a new Class Library project running on .NET 8. Name it Kickstart.Entities

Depending on your IDE, you may need to manually create a corresponding folder for the project.

Screenshot of the Kickstart.Entities project and its folder

Reference the project

Add a reference from the Kicskstart.Web project to the Kickstart.Entities project.

You can do this by adding a line like <ProjectReference Include="..\Kickstart.Entities\Kickstart.Entities.csproj" /> to the Kickstart.Web.csproj file, or through the UI of some editors.

If you are using Visual Studio the steps are:

  1. Right-click on Kickstart.Web in the solution explorer.
  2. Choose Add → Project reference.
  3. Select the box for Kickstart.Entities, then click OK.

Configure the project

Add the Kentico.Xperience.Core NuGet package to the Kickstart.Entities project. Make sure to match the version of Xperience you installed – 29.3.0 in this case.

Then edit Kickstart.Entities.csproj to add the CMS.AssemblyDiscoverableAttribute.

The CMS.AssemblyDiscoverableAttribute allows the Xperience API to recognize and register classes and implementations when the application starts.

XML
Kickstart.Entities.csproj


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

Generate the class

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

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

Bash
Generate code files


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

If you’d like to learn more about how to generate classes for different system objects created through the administration interface, we recommend this documentation page

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 Slogan.generated.cs file for the Kickstart.Slogan content type, located in the ~/ReusableContentTypes/Slogan folder of the Kickstart.Entities project.

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

Code generation is a task that you will likely be performing repeatedly over the course of development.

Check out our Automate regular tasks with PowerShell scripts guide to learn how you can automate and optimize this and other similar processes.

In the next step we will cover how to create a web channel, in order to display your content.

Previous step: Set up an Xperience by Kentico projectNext step: Add a website channel

Completed steps: 3 of 13