I could't find a working example on how to create a reusable contentitem online. It was something I needed to include in a NuGet package. I think this might be helpfull for others.
First create your contenttype in the UI, so you can generate the class, and have the CI file as a reference.
Next implement a class like the example below. To have your new contenttype created, add a module, and call your installer using ApplicationEvents.Initialized.Execute.
using CMS.ContentEngine;
using CMS.DataEngine;
using CMS.FormEngine;
namespace MyProject.FaqContent.Admin;
internal interface IFaqContentInstaller
{
void Install();
}
internal class FaqContentInstaller(IContentTypeManager contentTypeManager) : IFaqContentInstaller
{
private const string DISPLAY_NAME = "FAQ";
public void Install()
{
EnsureContentType();
}
private void EnsureContentType()
{
if (ContentTypeExists())
{
return;
}
var contentType = ContentTypeInfo.New(ContentTypeInfo.OBJECT_TYPE_CONTENTTYPE);
contentType.ClassName = FaqContent.CONTENT_TYPE_NAME;
contentType.ClassDisplayName = DISPLAY_NAME;
contentType.ClassTableName = FaqContent.CONTENT_TYPE_NAME.Replace(".", "_");
contentType.ClassType = ClassType.CONTENT_TYPE;
contentType.ClassContentTypeType = ClassContentTypeType.REUSABLE;
contentType.ClassWebPageHasUrl = false;
contentType.ClassHasUnmanagedDbSchema = false;
contentType.ClassIconClass = "xp-question-circle";
contentType.ClassShortName = "FaqContent";
contentTypeManager.Initialize(contentType);
var formInfo = GetClassFormInfo(contentType.ClassFormDefinition, GetFormFields());
contentType.ClassFormDefinition = formInfo.GetXmlDefinition();
contentType.Insert();
contentTypeManager.CreateTableStructures(contentType.ClassTableName);
}
private static FormInfo GetClassFormInfo(string contentTypeDefinition, IEnumerable<FormFieldInfo> fields)
{
var formInfo = new FormInfo(contentTypeDefinition);
foreach (var field in fields)
{
formInfo.AddFormItem(field);
}
return formInfo;
}
private static IEnumerable<FormFieldInfo> GetFormFields()
{
var fields = new List<FormFieldInfo>();
// Add FaqContentQuestion field (Text, Required)
var questionField = new FormFieldInfo
{
Name = "FaqContentQuestion",
AllowEmpty = false,
DataType = FieldDataType.Text,
Size = 200,
Enabled = true,
Visible = true,
Caption = "Resource Key",
DefaultValue = string.Empty,
};
QuestionField.SetPropertyValue(FormFieldPropertyEnum.FieldCaption, "Question");
QuestionField.SetPropertyValue(FormFieldPropertyEnum.FieldDescription, "Enter the question");
QuestionField.SetPropertyValue(FormFieldPropertyEnum.ExplanationTextAsHtml, "False");
QuestionField.SetPropertyValue(FormFieldPropertyEnum.FieldDescriptionAsHtml, "False");
QuestionField.Settings["controlname"] = "Kentico.Administration.TextInput";
fields.Add(questionField);
// Add FaqContentText field (Long Text, Required)
var textField = new FormFieldInfo
{
Name = "FaqContentAnswer",
AllowEmpty = false,
DataType = FieldDataType.LongText,
Enabled = true,
Visible = true,
Caption = "Localized Text",
DefaultValue = string.Empty
};
textField.SetPropertyValue(FormFieldPropertyEnum.FieldCaption, "Answer");
textField.SetPropertyValue(FormFieldPropertyEnum.FieldDescription, "Give the answer");
textField.SetPropertyValue(FormFieldPropertyEnum.ExplanationTextAsHtml, "False");
textField.SetPropertyValue(FormFieldPropertyEnum.FieldDescriptionAsHtml, "False");
textField.Settings["controlname"] = "Kentico.Administration.TextArea";
textField.Settings["MinRowsNumber"] = "3";
textField.Settings["MaxRowsNumber"] = "5";
fields.Add(textField);
return fields;
}
internal bool ContentTypeExists()
{
var contentType = DataClassInfoProvider.GetDataClassInfo(FaqContent.CONTENT_TYPE_NAME);
return contentType != null;
}
}
Environment
- Xperience by Kentico