Alternative Method to Display Category Name through Repeater Web Part with no access to the web proj

Lambert Chan asked on June 24, 2019 19:16

As title suggests, I'm trying to have a repeater web part show multiple pages with their respective category names through transformations.

From what I've seen so far Kentico does provide code to add a new transformation function to the system but I don't have access to the web project with visual studio and I'm wondering if there are alternatives to the provided method.

I've tried to use macro resolver but that only shows the category of the page itself and it won't show the loaded documents, nor can I seem to find a category name column in the database to load and call.

Correct Answer

Brenden Kehren answered on June 24, 2019 23:17

You have way too much information in there. You need something like this: (you prob don't need to pass input variables either as you can get them inside the method)

<%@ Import Namespace="CMS.DocumentEngine" %>
<%@ Import Namespace="CMS.Taxonomy" %>
<script runat="server>

        public string GetDocumentCategories(int documentId, string documentListAliasPath)
        {
          if (documentId < 1)
          {
            throw new Exception("Invalid document ID");
          }

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

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

      // Gets the categories of the specified page
      var categories = DocumentCategoryInfoProvider.GetDocumentCategories(documentId)
          .Columns("CMS_Category.CategoryID, CategoryDisplayName");

      foreach (CategoryInfo category in categories)
        {
          // Constructs links for the assigned categories
          // The links lead to a page containing a list of pages that belong to the same category, with the category ID in the query string
          int categoryId = category.CategoryID;
          string categoryName = category.CategoryDisplayName;

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

      return result;
    }
</script>
<article typeof="Article">
  <header>
    <h3> Category: <%# GetDocumentCategories(Eval<int>("DocumentID"), null) %> </h3>
    <h3> <%# Eval("Title") %> </h3>
    <h4><%# Eval("Authors") %></h4> 
  </header>
</article>
2 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on June 24, 2019 19:31

In any ascx transformation you can add the following:

<script runat="server>

</script>

Inside the script tags you can add any code you want. Keep in mind you may have to fully namespace your classes but either way it will work. Check out this post for some example code.

1 votesVote for this answer Mark as a Correct answer

Lambert Chan answered on June 24, 2019 20:19

That makes a lot of sense to just have the script inside the transformation as opposed to loading it in the system for use.

I apologize for being unfamiliar with the nuances of the script itself, but I have encountered this error

error CS1529: A using clause must precede all other elements defined in the namespace except extern alias declarations[TempITemplate.Template]: 

when I pasted the script provided and then called the function in the transformation.

Category: <%# GetDocumentCategories(Eval<int>("DocumentID"), null) %>

The only thing preceding the script is:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="CBA.WebControls.BaseCMSTransformation" %> <%@ Import Namespace="CBA" %>

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on June 24, 2019 22:30

What does your full transformation code look like?

0 votesVote for this answer Mark as a Correct answer

Lambert Chan answered on June 24, 2019 23:08

I took the code from the display category using transformations page and pasted within a script block in the transformation.

<%@ Control Language="C#" AutoEventWireup="true" Inherits="CBA.WebControls.BaseCMSTransformation" %>
<%@ Import Namespace="CBA" %>
<script runat="server">
using System;
using CMS.DocumentEngine;
using CMS.Taxonomy;
using CMS.Helpers;

namespace CMS.DocumentEngine.Web.UI
    {
      /// <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 page's alias path if one is not specified for the category list page
          if (documentListAliasPath == null)
          {
            documentListAliasPath = DocumentContext.CurrentAliasPath;
          }

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

          // Gets the categories of the specified page
          var categories = DocumentCategoryInfoProvider.GetDocumentCategories(documentId)
              .Columns("CMS_Category.CategoryID, CategoryDisplayName");

          foreach (CategoryInfo category in categories)
            {
              // Constructs links for the assigned categories
              // The links lead to a page containing a list of pages that belong to the same category, with the category ID in the query string
              int categoryId = category.CategoryID;
              string categoryName = category.CategoryDisplayName;

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

          return result;
        }
    }
}
</script>
<article typeof="Article">
  <header>
    <h3> Category: <%# GetDocumentCategories(Eval<int>("DocumentID"), null) %> </h3>
    <h3> <%# Eval("Title") %> </h3>
    <h4><%# Eval("Authors") %></h4> 
  </header>
</article>
0 votesVote for this answer Mark as a Correct answer

Lambert Chan answered on June 25, 2019 00:19

Thanks once again Brenden, that worked as intended, so instead of importing through the script, the import of documentengine and taxonomy would be at the top and then extension of the partial class CMSTransformation not needed.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.