, , ,

How to read Kentico Class Form Definition from code behind?

Posted by

To read the Kentico class form definition from the code-behind file, you can use the Kentico API to retrieve the form definition for a specific class. Here’s an example of how you can do it:

  1. Import the necessary Kentico namespaces at the top of your code-behind file:
   using CMS.DocumentEngine;
   using CMS.FormEngine;
  1. In your code-behind file, retrieve the class form definition using the FormHelper.GetFormDefinition method. This method takes two parameters: the class name and the site name (if applicable). Here’s an example:
   protected void Page_Load(object sender, EventArgs e)
   {
       // Specify the class name for which you want to retrieve the form definition
       string className = "Your.Class.Name";

       // Retrieve the form definition
       DataClassInfo classInfo = DataClassInfoProvider.GetDataClass(className);
       FormInfo formInfo = FormHelper.GetFormDefinition(classInfo.ClassFormDefinition);

       // Now you have the form definition, and you can work with it
       // For example, you can iterate through the form fields and access their properties
       foreach (FormFieldInfo fieldInfo in formInfo.GetFields(true))
       {
           // Access field properties
           string fieldName = fieldInfo.Name;
           string fieldCaption = fieldInfo.Caption;

           // Do something with the field properties
           // For example, you can add them to a collection or output them
           Console.WriteLine("Field Name: " + fieldName);
           Console.WriteLine("Field Caption: " + fieldCaption);
       }
   }

In the above example, replace "Your.Class.Name" with the actual name of the Kentico class for which you want to retrieve the form definition.

  1. Depending on your requirements, you can modify the code to access specific properties of the form fields or perform additional actions with the form definition.

Note that the above example assumes you are using C# as the programming language. If you are using VB.NET, the code will be slightly different, but the overall approach remains the same.