Create custom fields based on existing ones
Sitefinity CMS MVC widgets use fields to build their views both for list and detail mode. Depending on your requirements, you can customize these fields by changing the view of a specific field either in a single dynamic content MVC widget or in all dynamic content MVC widgets. In addition to changing the view of a field, you may want to add additional logic to the field. In this case, we recommend to to create custom fields. The following article explains how to create custom field that inherits the LongTextAreaField and register it using dependency injection.
Perform the following:
-
Open the your project in Visual Studio.
-
In the context menu of SitefinityWebApp, click Add» Classand name it
CustomLongTextField. -
Inherit the
LongTextAreaFieldbase field class. -
Under SitefinityWebAppadd a new folder and name it
Templates. -
In the context menu of folder
Templates, click Code» Code File and name itCustomLongTextAreaField.cshtml -
In the content of the file, add the following code:
HTML+Razor@model Telerik.Sitefinity.DynamicModules.Builder.Model.DynamicModuleField @@*Start @Model.Name field*@@ <div> <strong>Custom html goes here</strong> </div> @@*End @Model.Name field*@@ -
Open the
CustomLongTextField.csand add the following code:C#using Telerik.Sitefinity.DynamicModules.Builder.Model; using Telerik.Sitefinity.Frontend.DynamicContent.WidgetTemplates.Fields.Impl; namespace SitefinityWebApp { public class CustomLongTextField : LongTextAreaField { protected override string GetTemplatePath(DynamicModuleField field) { return CustomLongTextField.TemplatePath; } private const string TemplatePath = "~/Templates/CustomLongTextAreaField.cshtml"; } }The code above registers new template for the custom field by overriding the
GetTemplatePathmethod. -
Register the custom field using dependency injection. Perform the following:
-
Open the global application class.
If you do not have aGlobal.asaxclass in your project, in the context menu of SitefinityWebApp, click Add » New Item » Global Application Class. -
Inside the
Global.asax, register to theBootstrapper.Bootstrappedevent and useObjectFactoryclass to register the new custom field in the following way:C#using System; using Telerik.Microsoft.Practices.Unity; using Telerik.Sitefinity.Abstractions; using Telerik.Sitefinity.Frontend.DynamicContent.WidgetTemplates.Fields; using Telerik.Sitefinity.Frontend.DynamicContent.WidgetTemplates.Fields.Impl; namespace SitefinityWebApp { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped; } private void Bootstrapper_Bootstrapped(object sender, EventArgs e) { ObjectFactory.Container.RegisterType(typeof(Field), typeof(CustomLongTextField), typeof(LongTextAreaField).Name); } } }The
ObjectFactoryis a special class in Sitefinity CMS that wraps theIUnityContainer, part of the Microsoft Practices Unity Application Block. You use the Unity Application Block for inversion of control pattern.
-
-
Build the application.
You can use the new field in the dynamic content MVC widgets.