Example - Displaying categories in document lists

If you have documents that are organized in categories, you can display a list of categories that each document belongs to. You need to write a transformation 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: 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:

    
    
    
     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.

    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