
Editorial illustration.
Originally published 22 June 2023; rewritten and reviewed on 15 October 2026.
In a Kentico CMS project, sooner or later you hit a requirement where a custom .aspx.cs web part, scheduled task, or CSV export handler needs to know which fields exist on a Page Type, Custom Table, or BizForm. The temptation is to hard-code a string array of column names in C# and call it a day. That works right up until a content editor adds a new field in the Kentico admin UI, renames a caption, or changes a field’s required flag, and your custom code silently ignores it or crashes on a missing column.
Instead of maintaining a parallel list that drifts out of sync, read the form definition directly from the metadata Kentico already stores in CMS_Class.
Separating schema metadata from submitted records
A form definition (ClassFormDefinition) is an XML document describing the structure of a class: column names, data types, captions, default values, and validation rules. It is not a data record, and inspecting it does not verify whether the current visitor or caller is allowed to read actual rows from that table.
Keeping those two concerns separate saves you from accidental data leaks and messy permissions bugs later. In Kentico 12/13 (Portal Engine or MVC/Core), DataClassInfoProvider and FormInfo give you a clean way to inspect that schema from code-behind or a service class.
using System;
using System.Collections.Generic;
using CMS.DataEngine;
using CMS.FormEngine;
using CMS.Helpers;
public sealed class FieldMetadataDto
{
public string Name { get; set; }
public string Caption { get; set; }
public string DataType { get; set; }
public bool IsRequired { get; set; }
}
public static class ClassSchemaReader
{
public static IReadOnlyList<FieldMetadataDto> GetVisibleFields(string className)
{
if (string.IsNullOrWhiteSpace(className))
{
return Array.Empty<FieldMetadataDto>();
}
DataClassInfo dataClass = DataClassInfoProvider.GetDataClassInfo(className);
if (dataClass == null || string.IsNullOrWhiteSpace(dataClass.ClassFormDefinition))
{
return Array.Empty<FieldMetadataDto>();
}
var formInfo = new FormInfo(dataClass.ClassFormDefinition);
var fields = new List<FieldMetadataDto>();
// Exclude system fields (ItemID, ItemGUID, etc.) and category headers
foreach (FormFieldInfo field in formInfo.GetFields(includeCategories: false, includeSystem: false))
{
if (!field.Visible)
{
continue;
}
string rawCaption = field.GetPropertyValue(FormFieldPropertyEnum.FieldCaption);
string caption = string.IsNullOrWhiteSpace(rawCaption)
? field.Name
: ResHelper.LocalizeString(rawCaption);
fields.Add(new FieldMetadataDto
{
Name = field.Name,
Caption = caption,
DataType = field.DataType,
IsRequired = !field.AllowEmpty
});
}
return fields;
}
}
Wrapping DataClassInfoProvider and FormInfo behind a small adapter like ClassSchemaReader keeps version-specific Kentico APIs in one place. If you later upgrade from Kentico 12 to Xperience 13 (where Provider static methods shifted toward IInfoProvider<DataClassInfo> via dependency injection), you only touch one class instead of hunting through twenty .ascx.cs files.
Real-world gotchas in ClassFormDefinition
A few details trip developers up the first time they read FormInfo in production:
- System fields pollute exports: Calling
formInfo.GetFields(true, true)pulls in internal columns such asItemID,ItemCreatedBy,ItemModifiedWhen, andItemOrder. PassingincludeSystem: false(or filteringfield.System) keeps your UI or export headers focused on business fields. - Localization macros in captions: Editors often enter captions like
{$general.email$}or macro expressions in the Field Builder. If you outputfield.GetPropertyValue(FormFieldPropertyEnum.FieldCaption)raw, users will see literal{$...$}tokens. Passing it throughResHelper.LocalizeString(...)resolves resource strings cleanly. - Null or empty definitions: Coupled or system classes sometimes have an empty
ClassFormDefinitionstring. Guarding againstdataClass == nullandstring.IsNullOrWhiteSpace(dataClass.ClassFormDefinition)preventsXmlExceptioncrashes when someone passes an unexpected class code name. - Exact type vs display control:
field.DataTypetells you the SQL/engine type (text,longtext,integer,datetime), not the form control (DropDownListControl,RichTextEditor,UploadFile). If your logic depends on how the field is rendered, inspectfield.Settings["controlname"]as well.
Caching without stale schemas
Parsing XML on every page load is unnecessary overhead, especially on high-traffic pages. If you cache the IReadOnlyList<FieldMetadataDto> result, attach a Kentico cache dependency on the class record so any edit in the CMS admin clears the cache immediately:
var result = CacheHelper.Cache(
cs =>
{
if (cs.Cached)
{
cs.CacheDependency = CacheHelper.GetCacheDependency($"cms.class|byname|{className.ToLowerInvariant()}");
}
return ClassSchemaReader.GetVisibleFields(className);
},
new CacheSettings(60, "custom", "classschema", className.ToLowerInvariant())
);
Without that dependency key, you end up telling the client to wait an hour or recycle the IIS app pool every time they tweak a field label.
Keep internal metadata out of public endpoints
One last boundary check: never expose an unauthenticated .ashx handler or API controller that accepts an arbitrary className query parameter and returns the full ClassFormDefinition XML. Even if it contains no customer rows, returning internal table structures, hidden admin fields, and validation regexes gives attackers a free map of your schema. Keep schema inspection on the server, restrict any diagnostic views to authenticated administrators, and return only the minimal DTO your feature actually needs.