In this tutorial, you will learn how to create a custom dynamic property in Episerver. A client, had a requirement for a user to be able to dynamically add different forms onto a page. The best way to do this within v6 is via dynamic controls. Within any rich-text editor, a dynamic control can be added by content editors. By creating custom dynamic controls, content editors can add custom 'things' onto a page. Dynamic controls can be thought of as adding components to a page.
The issue with hardcoding dynamic control values within textboxes is that they become hard to manage in large sites. It's not easy to tell where a dynamic control is used. For this project, the site had thousands of pages. The content editor could drop forms anywhere, this posed a potential maintenance nightmare π¨π¨π¨
When an editor added a form, they would pick which one they wanted to render from a list. This would then hardcode the form ID into the text area. Forms were created and delete frequently. This meant that after a few months, the site would have contained a lot of controls dotted around the site in unknown areas with hardcoded IDs that no longer exist.
To create an easier to manage solution, I came up with a solution that involved using a dropped dynamic control that would generate a GUID. A content editor could then use that GUID in a separate system in the Episerver admin to associate a list of controls against the guid
. When a page loaded, a look-up would be made to render the things
. If the control had no matching buckets, it would not render out anything. Solving my problem.
To build this control, you will need to create a dynamic control as normal, however, with one change :
In DynamicContentPlugIn
, add a Url
and Area
attribute. The Url
needs to point to a user control that inherits from DynamicContentEditControl
. Next, create the user control that will be used as our custom setting display. All this control will do is, on creation, automatically create a Guid
and display it:
When inheriting from DynamicContentEditControl
, you need to implement the PrepareForSave
method. In PrepareForSave
you need to save the data so it can be used in the display control. This is done using Control.Properties[].Value
:
One thing to note is that Content
will be null
if you try to use it in the constructor. Everything else should be pretty standard. On page creation, check if a GUID
exists for that page, if it does... use it. Otherwise, create it.
As always, here's the code toΒ download. Happy Coding π€