---
title: Example - Displaying categories in document lists
related:
  - https://docs.kentico.com/k8/developing-websites/loading-and-displaying-data-on-websites/writing-transformations.md
  - https://docs.kentico.com/k8/custom-development/miscellaneous-custom-development-tasks/adding-custom-methods-to-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 have documents that are organized in [categories](https://docs.kentico.com/k8/managing-website-content/working-with-documents-and-pages/categorizing-documents.md), you can display a list of categories that each document belongs to. You need to write a [transformation](https://docs.kentico.com/k8/developing-websites/loading-and-displaying-data-on-websites/writing-transformations.md) method for displaying a list of categories assigned to a given document.

## Displaying document categories using a custom transformation function

1. Open your web project in Visual Studio.

   > **Note:** **Note:** If you have already extended the **CMSTransformations** partial class when defining other transformation methods, you can add the method for displaying categories there without creating a new class.
2. Create a new folder under the **App\_Code** folder (or **CMSApp\_AppCode -> Old\_App\_Code** if you installed the project as a web application) 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**.
5. Remove the default content of the class and enter the following code:

   ```csharp

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;

   using System.Data;

   using CMS.DocumentEngine;
   using CMS.Taxonomy;
   using CMS.Helpers;

   namespace CMS.Controls
   {
       /// <summary>
       /// Extends the CMSTransformation partial class.
       /// </summary>
       public partial class CMSTransformation
       {
           public string GetDocumentCategories(int documentId, string documentListAliasPath)
           {
               if (documentId < 1)
               {
                   throw new Exception("Invalid document ID");
               }

               // Uses the current document's alias path if one is not specified
               if (documentListAliasPath == null)
               {
                   documentListAliasPath = DocumentContext.CurrentAliasPath;
               }

               // Initializes the HTML code result
               string result = "";

               // Gets the categories of the specified document
               DataSet ds = CategoryInfoProvider.GetDocumentCategories(documentId, null, null, 0, "CMS_Category.CategoryID, CategoryDisplayName");

               if (!DataHelper.DataSourceIsEmpty(ds))
               {
                   foreach (DataRow row in ds.Tables[0].Rows)
                   {
                       // Constructs links for the assigned categories
                       // The links lead to a page containing a list of documents that belong to the same category, with the category ID in the query string
                       int categoryId = ValidationHelper.GetInteger(row["CategoryID"], 0);
                       string categoryName = ValidationHelper.GetString(row["CategoryDisplayName"], null);

                       result += "<a href=\"" + URLHelper.ResolveUrl(DocumentURLProvider.GetUrl(documentListAliasPath));
                       result += "?category=" + categoryId;
                       result += "\">" + categoryName + "</a>&nbsp;";
                   }
               }

               return result;
           }
       }
   }

   ```
6. Save the class. **Build** the project if it is installed as a web application.
7. Call the method in the code of your transformation.

   ```xml title="Example"

   <strong>Categories:</strong> <%# GetDocumentCategories(Eval<int>("DocumentID"), null) %>

   ```

The method takes the following arguments:

- **int DocumentID** - the ID of the document whose categories you want to retrieve.
- **string DocumentListAliasPath** - path of the page that contains the list of documents (filtered according to the _category_ query string parameter). If you set the argument to _null_, the method uses the current document.

If you view the output of the transformation, you can see the categories to which the displayed document belongs.

![Transformation display an article document with a list of associated categories](https://docs.kentico.com/docsassets/k8/example-displaying-categories-in-document-lists/Document_Categories.png "Transformation display an article document with a list of associated categories")
