Loading

EDITING CUSTOM MODULE DATA - Part 2

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.


Comments
https://www.couponnx.in/price-check/
This really is actually fascinating, That you are a quite specialist blogger. Ive joined your rss feed and sit up for looking for more of your excellent post. Also, I have shared your web site in my social networks!
6/21/2017 6:35:45 PM

MAHRESULT.NIC.IN
The Maharashtra State Board of Technical Education (MSBTE) declared the results for the MSBTE Summer Diploma examinations on Thursday. The candidates can check their grades on the official website msbte.com. As has been observed over the course of the past few weeks, the dates and times of result announcements have been frequently changed. The information above has not been independently verified. However, this article will continue to be updated to reflect official updates as and when they come in.
6/11/2017 9:48:12 PM

facetime for windows
facetime for windows
6/5/2017 11:59:39 AM

facetime for android
facetime for android
6/2/2017 11:20:03 AM

cbseresults.nic.in
The wait of students is finally over as the Central Board of Secondary Education has announced the CBSE class 10th results. The students can check their results through the official websites of the CBSE Board.
5/17/2017 1:58:21 AM

up results nic in
The results for the Class 10th and Class 12th annual examination of the Uttar Pradesh Board will be announced in May.
5/11/2017 8:53:43 AM

videoder dowload
Videoder is an android app that lets you download music and videos from youtube, facebook, instagram, vimeo, dailymotion etc for free.
4/7/2017 9:36:07 AM

essay on time
Thank you so much for having the second part of the module data that is useful for me and help me a lot to complete my project on time. I really appreciate your great sharing and will visit yours again for upcoming projects help.
3/20/2017 12:53:31 PM

examresults.ap.nic.in
Are you waiting for AP Inter Results 2017? You visit the right place to know the information of Intermediate Results
3/19/2017 9:39:05 AM

Magnus
I'm trying to implement your code... it displays the fields from the class just fine... but when i click the submit button i can track that both AfterValidate and BeforeSave is raised. OnValidationFailed is not raised so validation goes fine...
12/9/2016 1:54:38 PM

Daniel
Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how could we communicate?
12/1/2016 7:15:09 AM

John
This really is actually fascinating, That you are a quite specialist blogger. Ive joined your rss feed and sit up for looking for more of your excellent post. Also, I have shared your web site in my social networks!
10/26/2016 9:40:54 AM

 Security code