---
title: Generating classes for Xperience objects
related:
  - https://docs.kentico.com/13/developing-websites/retrieving-content.md
  - https://docs.kentico.com/13/developing-websites/retrieving-content/displaying-page-attachments.md
  - https://docs.kentico.com/13/custom-development/working-with-pages-in-the-api.md
  - https://docs.kentico.com/13/custom-development/retrieving-database-data-using-objectquery-api.md
  - https://docs.kentico.com/13/developing-websites/defining-website-content-structure/managing-page-types.md
  - https://docs.kentico.com/13/developing-websites/defining-website-content-structure/managing-custom-tables.md
  - https://docs.kentico.com/13/managing-website-content/forms.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

This page describes how to use code generators to prepare wrapper classes for working with specific object types ([Page types](https://docs.kentico.com/13/developing-websites/defining-website-content-structure/managing-page-types.md), [Custom tables](https://docs.kentico.com/13/developing-websites/defining-website-content-structure/managing-custom-tables.md) and [Forms](https://docs.kentico.com/13/managing-website-content/forms.md)).

Code generators are available for the following objects, in their respective applications:

- [Page types](https://docs.kentico.com/13/developing-websites/defining-website-content-structure/managing-page-types.md) – allow you to generate a class with the page type's fields as properties.
  - [Generate code](#generating-code-for-page-types) for individual page types on the **Code** tab of the specific types.
  - Generate code for all page types on the **Code** tab of the **Page** **types** application.

    > **Info:** **Product page types**
    >
    > Code generators also work for  [product page types](https://docs.kentico.com/13/e-commerce-features/configuring-on-line-stores/configuring-product-page-types.md). However, unlike page types, which inherit from the _TreeNode_ class, product page types inherit from the _SKUTreeNode_ class.
- [Custom tables](https://docs.kentico.com/13/developing-websites/defining-website-content-structure/managing-custom-tables.md) – allow you to generate a class with the custom table's fields as properties.
  - Generate code for individual custom tables on the **Code** tab of the given custom table's editing interface.
- [Forms](https://docs.kentico.com/13/managing-website-content/forms.md) – allow you to generate a class with the form's fields as properties.
  - Generate code for individual forms on the **Code** tab of the given form's editing interface.

Code generators allow you to create classes for working with specific objects types (Page types, Custom tables, Forms) in the API.

## Working with page type code generators

The page type code generators allow you to generate classes holding the properties of your custom page types. The generated properties represent the page type fields.

### Generating code for page types

1. Choose whether you want to generate code for  a single  or all site page types.
   - For a single page type:
     1. In the **Page types** application, edit () a page type.
     2. Navigate to the **Code** tab.
   - For all site page types:
     1. In the **Page types** application, navigate to the **Code** tab.
     2. Select a **Site**.
     3. (Optional) If you want to generate code for page types without any custom fields, enable the **Include page types without fields** check box.
2. (Optional) Select a different default folder to place the code files in.
3. Click **Save code**.

   > **Info:** **Note**: The generated class names are not guaranteed to be unique. The system can in certain cases generate multiple classes with the same name. If this occurs, you need to rename the classes manually.

The system generates the page type classes in the specified folder. You can find examples of working with the generated classes on:

- [Displaying page content](https://docs.kentico.com/13/developing-websites/retrieving-content/displaying-page-content.md) – learn how to retrieve pages, filter based on page taxonomy ([content tree](https://docs.kentico.com/13/developing-websites/defining-website-content-structure.md) structure, [categories](https://docs.kentico.com/13/managing-website-content/working-with-pages/categorizing-pages/assigning-pages-to-categories.md), [tags](https://docs.kentico.com/13/managing-website-content/working-with-pages/categorizing-pages/tagging-pages.md)), and how to access individual fields of the retrieved pages.
- [Retrieving content](https://docs.kentico.com/13/developing-websites/retrieving-content.md) – learn how to retrieve and filter user form submissions, custom table data, and objects associated with custom modules.

### Including the generated page type wrapper classes in your live site project

To use the generated code on the live site, you need to copy the files to the project folder of your live site (MVC) application.

By default, the generated code files are stored in the _\~/Old\_App\_Code/CMSClasses_ folder of your administration project.

1. In your live site project, create a new **Generated** folder under your **Models** folder, e.g., _C:\inetpub\wwwroot\Xperience13\MySite\Models\Generated_.
2. Copy the classes that represent your page types to the **Generated** folder.
3. Click the **Show all files** button to see the files you have copied in the solution.
4. Select the **Models/Generated** folder, right-click to open the context menu, and select **Include In Project**.

> **Note:** **Allowing discovery of the generated classes in custom assemblies**
>
> When using the generated code files in a separate library or external application, you need to allow the system to detect the classes. Make sure class discovery is enabled for the project – see [Adding custom assemblies](https://docs.kentico.com/13/custom-development/adding-custom-assemblies.md).
>
> **Note**: Do not directly add generated code files into external projects that compile into a different output type than a DLL assembly (for example console applications). The system cannot discover the code in these cases. Instead, add the code into a _Class Library_ project with the _AssemblyDiscoverable_ attribute and reference the assembly from your project.

## Customizing generated classes

In most scenarios, we do **not recommended directly modifying the generated classes**.

The classes are generated as [partial classes](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods). This means that you can extend them in a separate code file. Using this approach, you avoid the need to merge custom changes made to the generated code every time the class is generated again after the object's properties are changed.

### Example of customizing generated classes

```csharp title="Articles.generated.cs - generated class"

namespace CMS.DocumentEngine.Types.Custom
{
    public partial class Article : TreeNode
    {
        ...

        /// <summary>
        /// Article name.
        /// </summary>
        [DatabaseField]
        public string ArticleTitle
        {
            get
            {
                return ValidationHelper.GetString(GetValue("ArticleTitle"), "");
            }
            set
            {
                SetValue("ArticleTitle", value);
            }
        }
        ...
    }
}

```

```csharp title="Articles.cs - extending the generated class"

namespace CMS.DocumentEngine.Types.Custom
{
    /// <summary>
    /// Custom article page type class.
    /// </summary>
    public partial class Article : TreeNode
    {
        /// <summary>
        /// Loads sample data into this object.
        /// </summary>
        public void LoadSampleData()
        {
            ArticleTitle = "Sample article title";
            ArticleSummary = "Sample article summary";
            ArticleText = "Sample article text";
        }
    }
}

```

### Using nullable properties for non-required fields

Page type, custom table or form fields whose **Required** setting is disabled may have _null_ values in the database. However, the code generators create properties with non-nullable types for such fields. Instead, a default value is typically loaded in cases where the value is null, for example 0 for numeric types.

If you wish to have properties with [Nullable types](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/nullable-types/) in the code representing your page, custom table or form items, you need to manually adjust the code after generating.

1. Change the type of the given properties to a nullable one (using the **T?** syntax) in the **Properties** region of the generated class.
2. Adjust the [get property accessor](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/get) for each property:
   - Remove the default **ValidationHelper.Get** call (_GetInteger_, _GetBoolean_, etc.), which replaces null with a default value.
   - Cast the result of the **GetValue** method to the appropriate nullable type.
3. Change the type of the matching properties within the nested **\*Fields** class.

```csharp title="Example - Nullable integer property for a non-required field"

// Example of a nullable integer field
[DatabaseField]
public int? IntegerField
{
    get
    {
        // Adjusted loading of the value (without replacement of null with a default value)
        return GetValue("IntegerField") as int?;
    }
    set
    {
        SetValue("IntegerField", value);
    }
}

...

[RegisterAllProperties]
public partial class ItemFields : AbstractHierarchicalObject<ItemFields>
{

    ...

    // Adjusted nullable property within the nested *Fields class
    public int? IntegerField
    {
        get
        {
            return mInstance.IntegerField;
        }
        set
        {
            mInstance.IntegerField = value;
        }
    }

    ...

```

> **Note:** **Note**: Even though we generally recommend using partial classes to extend generated code in separate files, this cannot be done for modifications of property types. As a result, your code adjustments may be overwritten if you generate the code of the given object again at a later time. To preserve your changes, you need to manually update the re-generated code files.
