EDITING CUSTOM MODULE DATA - Part 2

#Custom software development #Kentico development #.NET #Kentico Kontent

SHARE

 
Recently I've shared an idea of how you could provide live site users with edit interface for custom module data. As you might know, perfection has no boundaries... This time I want to share some improvement, those will allow you to reuse the same web part for literally any object within your application.

 
I'm my first post on the related topic I've suggested using UI Form control in order to create/edit custom (or even native) objects within you system. It works perfectly fine with any particular object, but there is a limitation: you have to know what object you are going to work with beforehand. That's good, but not generic enough. So I went further and implemented class agnostic web part.
 
There were two challenges during implementation:
  • Instantiate object knowing just class code name when creating new object
  • Retrieve object data knowing just class code name and ID of an object when updating one
 
In first case ObjectTypeManager and a little bit of .net reflection were really useful.
In second case I knew ObjectQuery is the right path to go, but struggled with finding appropriate methods for data I have. Finally it turned to be really simple.
 
Following is entire code of my custom web part:
 

public partial class CMSWebParts_MyWebParts_EditObject : CMSAbstractWebPart
{
    public string Class
    {
        get
        {
            return ValidationHelper.GetString(GetValue("Class"), "");
        }
        set
        {
            SetValue("Class", value);
        }
    }

    public string Layout
    {
        get
        {
            return ValidationHelper.GetString(GetValue("Layout"), "");
        }
        set
        {
            SetValue("Layout", value);
        }
    }

    protected override void OnInit(EventArgs e)
    {
        var id = -1;
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            id = int.Parse(Request.QueryString["id"]);
        }

        var obj = ObjectTypeManager.GetTypeInfo(this.Class);

        BaseInfo bi = null;

        if (id > 0)
        {
            var editObj = new ObjectQuery(this.Class)
                .WhereID(obj.ClassStructureInfo.IDColumn, id)
                .FirstOrDefault();
            bi = (BaseInfo)editObj;
        }
        else
        {
            var newObj = Activator.CreateInstance(obj.InfoType);
            bi = (BaseInfo)newObj;
        }

        EditForm.EditedObject = bi;
        EditForm.Mode = id > 0 ? FormModeEnum.Update : FormModeEnum.Insert;
        EditForm.ObjectSiteID = SiteContext.CurrentSiteID;
        EditForm.ObjectType = this.Class;
        EditForm.MessagesPlaceHolder = new CMS.ExtendedControls.MessagesPlaceHolder();

        EditForm.AlternativeFormName = AlternativeFormInfoProvider.GetAlternativeFormInfo(this.Layout).FormName;
    }

    public override void OnContentLoaded()
    {
        base.OnContentLoaded();
        SetupControl();
    }

    protected void SetupControl()
    {
        if (StopProcessing)
        {
            EditForm.StopProcessing = true;
        }
    }
}


 
This control is generic enough to work with any object you need. Class property allows you to select object class and Layout allows you to specify alternative layout for your form. Also you don't need to adjust your code behind whenever you add new field to the object UI Form will take care about it.

Please, feel free to share your thought, ideas, feedback or experiance on this topic.

Author

Check other articles

5 5

What our clients say

Bits Orchestra team are outstanding developers‚Äč. They listen carefully to our business needs and easily turns our business objectives into a well thought out and executed development effort. Roman is very bright and definitely the most capable developer that has worked on our site. He is not only a Kentico expert but has successfully tackled other complicated development assignments demonstrating expertise in both front and backend development. Roman takes initiative to suggest enhancements that make site maintenance easier while improving the customer experience. The team is very responsive to our work requests and has great follow up. They have also worked very business partners and this has reflected positively on our company. Roman is a true partner for us and a tremendous asset to our organization. We will continue to work with them and would highly recommend Roman and his team for your development needs. He and his team will exceed your expectations!
 Alan Lehmann
Alan Lehmann
President at In energy sector

What our clients say

The Bits Orchestra team does excellent work. They are always available and I appreciate our frequent calls and screen-shares together. Their dedication to the projects and knowledge of Kentico is outstanding. They truly care about the quality of their work, and became a part of our team easily!
Shena Lowe
Shena Lowe
Managing Partner at Consensus Interactive

What our clients say

We hired Roman for a Kentico analysis project and have been very satisfied. He is very skilled and professional. We are looking to hire him and his team again on future projects.
Sylvain Audet
Sylvain Audet
CEO at MyDevPartner.com

What our clients say

Roman and team have taken over an existing Kentico EMS site for a large US Oil Company. So far, they have handled every single request that we have thrown at them and these were diverse, challenging, often bespoke, usually urgent and almost daily, over the last 11 months. Their work is of an extremely high quality, they are capable, quick and we have great confidence in the support that we are getting.
Jon Hollis
Jon Hollis
Head of Web Development at confidential

What our clients say

Bits Orchestra team was very helpful, they had a good understanding of the brief and deep knowledge of the system. They were always keen to provide advice and recommendations that benefit the project substantially.
Ramon Lapenta
Ramon Lapenta
Senior Front End Developer at Cyber-Duck Ltd