Despite the fact that Kentico provides nice interface for managing data inside of the custom modules, sometimes there is a need to provide user to do so on the live site. There are appropriate web parts for editing pages, working with online forms or custom tables, but I couldn't find any control that provides similar functionality for custom module class. In this post I want to share with you solution I found.
Problem
Let's consider a situation when you need to expose editing interface for custom module class (or any other type of object) on the live site. There might be a couple of good reasons for doing this, for example there is a role responsible for management of data for a single class, so there is no need to overwhelm that role by exposing Kentico Admin interface, as it could be confusing for non-technical staff.
Solution
You can always implement you custom control that will allow you to create/update some object within a system. But what if you need to add a new field at some point of time?! - Yes, you'll need to update you custom control accordingly and promote it through all environments. It is not that exciting when we are talking about adding a single field.
So what would be the approach to take? UI Form will solve that problem. This is a great control that can work with any class as well as it knows how to handle alternative forms. There are just a couple of simple steps you need to take in order to build edit form for your object:
1. Create custom user control or web part
2. Add UI Form to it:
<cms:uiform runat="server" id="EditForm" islivesite="true">
</cms:uiform>
3. Initialize vital properties for UI Form:
EditForm.EditedObject = myObj;
EditForm.Mode = CMS.ExtendedControls.FormModeEnum.Update;
EditForm.ObjectSiteID = SiteContext.CurrentSiteID;
EditForm.ObjectType = "className";
EditForm.AlternativeFormName = "formCodeName";
In case you need to create new record substitute firs two lines of code with the following:
EditForm.EditedObject = new MyCustomClass();
EditForm.Mode = CMS.ExtendedControls.FormModeEnum.Insert;
You'll have to place this code in init or load handlers of your control/web part
4. Add some logic that will alert user if operation has been completed successfully or there was some error
5. Make sure to add security checks, so only authorized users can make changes
Conclusion
Now you have a flexible control that you can change without code modifications, but adjusting alternative form or adding new field. It is always easier to reuse something existing and well tested instead of inventing something new. This is a case when less is more!