---
title: Adding custom methods to transformations
related:
  - https://docs.kentico.com/k10/developing-websites/loading-and-displaying-data-on-websites/writing-transformations.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).

If you need to add custom logic into your [transformations](https://docs.kentico.com/k10/developing-websites/loading-and-displaying-data-on-websites/writing-transformations.md), you can implement custom methods and call them in the transformation code. Custom transformation methods allow you to process field values, display data in custom formats, add conditions, and more.

To create custom methods for ASCX transformations:

1. Extend the **CMSTransformation** partial class in the **App\_Code** folder of your web project.
2. Add the definitions of the methods inside the class.

> **Note:** If you wish to add custom functionality for **Text** transformations, implement [custom macro methods](https://docs.kentico.com/k10/macro-expressions/extending-the-macro-engine/registering-custom-macro-methods.md).

The following example demonstrates how to create a custom method that trims text values to a specified number of characters.

## Writing the custom transformation method

1. Open your Kentico web project in Visual Studio.
2. Create a new folder under the **App\_Code** folder (or **Old\_App\_Code** on web application installations) and name it _CustomTransformationMethods_.
3. Right-click the folder and select **Add -> Add new Item**.
4. Create a new **Class**and name it **CMSTransformation.cs**.

   > **Note:** **Note**: Currently, you must develop transformation methods in C#.
5. Remove the default content of the class (apart from the basic references) and enter the following code instead:

   ```csharp

   namespace CMS.DocumentEngine.Web.UI
   {
       /// <summary>
       /// Extends the CMSTransformation partial class.
       /// </summary>
       public partial class CMSTransformation
       {
           /// <summary>
           /// Trims text values to the specified length.
           /// </summary>
           /// <param name="txtValue">Original text to be trimmed</param>
           /// <param name="leftChars">Number of characters to be returned</param>
           public string CustomTrimText(object txtValue, int leftChars)
           {
               // Checks that text is not null
               if (txtValue == null | txtValue == DBNull.Value)
               {
                   return "";
               }
               else
               {
                   string txt = (string)txtValue;

                   // Returns a substring if the text is longer than specified
                   if (txt.Length <= leftChars)
                   {
                       return txt;
                   }
                   else
                   {
                       return txt.Substring(0, leftChars) + "...";
                   }
               }
           }
       }
   } 

   ```
6. **Save** the file. **Build** the project if it is installed as a web application.

## Calling the custom method in ASCX transformations

1. Sign in to the Kentico administration interface.
2. Open the **Page types** application.
3. **Edit** () the **Corporate site - Transformations** page type.
4. Select the **Transformations** tab.
5. Edit the **BlogsList** transformation and change line 5 of its code to the following:

   ```xml

   ..
   <%# CustomTrimText(Eval("BlogDescription"), 50) %>
   ..

   ```

   ![Calling a custom transformation method](https://docs.kentico.com/docsassets/k10/adding-custom-methods-to-transformations/Custom_Transformation_Method.png "Calling a custom transformation method")
6. Click **Save** to confirm the modification.

To check the result, click the **Preview** button, enter _/Community/Blog&#x73;_&#x69;nto the path textbox on the preview bar and **Refresh** () the page section. In the preview of the blogs page that uses the transformation (on the sample Corporate site), you can see the text of the blog description truncated to the first 50 characters.

![Previewing the result of the custom transformation method](https://docs.kentico.com/docsassets/k10/adding-custom-methods-to-transformations/Custom_Transformation_Method_Result.png "Previewing the result of the custom transformation method")
