Creating Your Own Custom Dynamic Property Settings Form

We had a requirement for a user to be able to dynamically add different ‘things’ to a page; think of things such as a contact detail from an address book or business locations.  The tricky part of the problem was that these ‘things’ had to be able to be dropped, in multiple places, around the site by content editors within page content and that over time could be deleted.  If I used a normal picker that stored an Id of an asset on the page, after a few months I would have ended up with a site full of obsolete controls dotted around the site in unknown areas pointing to asset Id’s that no longer existed, data that would have been deleted, e.g. Id stored within EPi for assets that no longer existed.

Without going into the full requirements, 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 admin to create a list of ‘things’ that would be displayed on the page.  If an asset was deleted it would automatically be removed from a list rather than having an obsolete reference to it in a load of dynamic controls dotted around the site.

In order to do this, I had to create a dynamic control with a custom interface. When I Googled this, I couldn’t really find any good tutorials so I thought I would create this.  First you need to create a dynamic control as normal but with one change :

  [DynamicContentPlugIn(
DisplayName = "Contact List",
Description = "Displays a list of configurable contacts.",
ViewUrl = "~/DynamicContent/Contact.ascx",
Url = "~/DynamicContent/ContactSettings.ascx",
Area = PlugInArea.DynamicContent)
]
public partial class ContactList : EPiServer.UserControlBase
{
private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public string ListId { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
}

In your DynamicContentPlugIn you need to add a Url and Area attribute.  The Url needs to point to a user control that inherits from DynamicContentEditControl.  Next, we need to create the user control that will be used as our custom setting display.  All my control will do is, on creation, automatically create a Guid and display it.

  public partial class ContactSettings : DynamicContentEditControl
{
private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public Guid PageId { get; set; }
private const string PropertyName = "PageId";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
object data = null;
if (Content.Properties[PropertyName] != null)
{
data = Content.Properties[PropertyName].Value;
PageId = Guid.Parse(data.ToString());
}
if (data == null || string.IsNullOrEmpty(data.ToString()))
{
PageId = Guid.NewGuid();
}
}
catch (Exception ex)
{
Logger.Error(ex);
}
}
}
public override void PrepareForSave()
{
Content.Properties[PropertyName].Value = PageId.ToString();
}
}

When inheriting from DynamicContentEditControl you need to implement the PrepareForSave method.  In PrepareForSave you need to save the form data entered in your form 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.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts