---
title: Code generators
related:
  - https://docs.kentico.com/k82/custom-development/working-with-pages-in-the-api.md
  - https://docs.kentico.com/k82/developing-websites/defining-website-data-structure/page-types.md
  - https://docs.kentico.com/k82/developing-websites/defining-website-data-structure/custom-tables.md
  - https://docs.kentico.com/k82/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 topic describes how you can better work with [page types](https://docs.kentico.com/k82/developing-websites/defining-website-data-structure/page-types.md), [custom tables](https://docs.kentico.com/k82/developing-websites/defining-website-data-structure/custom-tables.md) and [forms](https://docs.kentico.com/k82/managing-website-content/forms.md) in the API.

Page data is stored in the _TreeNode_ object. This object contains general page properties and also page type specific properties. The page type specific data is [available through](https://docs.kentico.com/k82/custom-development/working-with-pages-in-the-api.md) the _GetValue(\<PageTypeField>)_ method. This means that you need to know the exact name of the page type fields that you want to access when working with them.

Code generators automatically provide a C# class which has these fields defined as properties. You can then use these properties directly in the code.

Code generators are available on the **Code tab** of the following objects, in their respective applications:

- [Page types](https://docs.kentico.com/k82/developing-websites/defining-website-data-structure/page-types.md)
- [Custom tables](https://docs.kentico.com/k82/developing-websites/defining-website-data-structure/custom-tables.md)
- [Forms](https://docs.kentico.com/k82/managing-website-content/forms.md)

![Class generated on the Code tab of the News page type](https://docs.kentico.com/docsassets/k82/code-generators/code_tab_news_page_type.png "Class generated on the Code tab of the News page type")

## Working with code generators

The following best practice uses page type objects as examples, but the same also applies to custom tables and forms.

1. Create a custom [Page type](https://docs.kentico.com/k82/developing-websites/defining-website-data-structure/page-types.md).
2. Include file the from **Code** tab in your solution. Based on your scenario, you can include the file in:
   1. _CMSApp\_MVC/Models_
   2. _CMSApp\_AppCode_
   3. Libraries
   4. Other location
3. (Optional) If you want to use the code files in a separate library or external application, allow the system to detect the generated classes in a custom assembly. Add the 'AssemblyDiscoverable' assembly attribute in the library/application project's _AssemblyInfo.cs_ file:

   ```csharp

   using CMS;
   ...
   [assembly: AssemblyDiscoverable] 

   ```
4. Use the generated properties in your code.
5. Build your solution.

## Page type example

The following example requires the class generated for the _CMS.News_ page type included in the solution.

```csharp

// Get news pages
InfoDataSet<News> pages = DocumentHelper.GetDocuments<News>()
    .Path("/News", PathTypeEnum.Children)
    .OnCurrentSite()
    .TypedResult;

// Get a news page
News result = DocumentHelper.GetDocuments<News>()
    .Path("/News/" + aliasPath)
    .OnCurrentSite()
    .FirstObject;

```

You can find working examples for the News page type in the sample MVC News controller in the _CMSApp\_MVC_ project.

## Custom table example

The following example requires the class generated for the _SampleTable_ custom table included in the solution.

```csharp

// Get all custom table items
InfoDataSet<SampleTableItem> items = CustomTableItemProvider.GetItems<SampleTableItem>().TypedResult;

// Get a single custom table item
SampleTableItem item = CustomTableItemProvider.GetItem<SampleTableItem>(1);

```

## Form example

The following example requires the class generated for the _ContactUs_ form included in the solution.

```csharp

// Get all form items
InfoDataSet<ContactUsItem> items = BizFormItemProvider.GetItems<ContactUsItem>().TypedResult;

// Get a single form item
ContactUsItem item = BizFormItemProvider.GetItem<ContactUsItem>(1);

```

## Customizing generated classes

It's not recommended to modify the classes generated by Code generators as they contain object properties only.

The classes are generated as _partial classes._ This means that youcan extend them in your custom code. Using this approach, you will avoid having to merge custom changes made to the generated code. You would have to do this every time the object's properties changed and you'd have to generate the class again.

### Customization example

```csharp title="News.fields.cs - generated class"

public partial class News : TreeNode
{
    ...

    #region "Properties"

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

```

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

/// <summary>
/// Custom news document type class.
/// </summary>
public partial class News : TreeNode
{
    /// <summary>
    /// Load this object with sample data.
    /// </summary>
    public void LoadSampleData()
    {
        NewsTitle = "Sample news title";
        NewsReleaseDate = new DateTime(2014, 8, 22);
        NewsSummary = "Sample news summary";
        NewsText = "Sample news text";
    }
}

```
