Custom Table Event Handler

Laura Frese asked on November 2, 2015 22:51

I am trying to catch Insert, Update, Delete events on Custom Tables. I am using some of the code from the App_Code/Samples/Modules/SampleHandlerModule.cs file, however I am unable to capture the events when they happen. It looks like Init() isnt even being called. Any suggestions? Do I need to name my class something different?

My file (CustomTableInterceptor.cs) in App_Code has the following code:

public partial class CMSModuleLoader
{
    private class CustomTableInterceptor : CMSLoaderAttribute
    {
        public override void Init()
        {
            EventLogProvider.LogEvent(EventType.INFORMATION, "CustomTableInterceptor", "", eventDescription: "Message: hello world");
            CustomTableInfo.TYPEINFO.Events.Update.After += GetTableInfo; 
        }

        protected void GetTableInfo(object sender, ObjectEventArgs e)
        {
        }
     }
 }

Correct Answer

Brenden Kehren answered on November 6, 2015 16:28

I believe you're syntax is incomplete/incorrect. As Josh stated you're currently looking to handle events for creating a new custom table, NOT for an actual row in the custom table. See below:

using CMS.Base;
using CMS.CustomTables;
using CMS.DataEngine;
using CMS.DocumentEngine;

[CustomTableEvents]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Attribute class that ensures the loading of custom handlers.
    /// </summary>
    private class CustomTableEventsAttribute : CMSLoaderAttribute
    {
        /// <summary>
        /// The system executes the Init method of the CMSModuleLoader attributes when the application starts.
        /// </summary>
        public override void Init()
        {
            // Assigns custom handlers to events
            ObjectEvents.Update.After += Update_After;
        }

        private void Update_After(object sender, ObjectEventArgs e)
        {
            switch (e.Object.TypeInfo.ObjectType)
            {
                case "customtableitem.yournamespace.yourclassname":
                    // do your work here
                    break;
                default:
                    break;
            }
        }
    }
}
2 votesVote for this answer Unmark Correct answer

Recent Answers


Joshua Adams answered on November 2, 2015 23:04

Do you have the correct syntax outside of the partial class? aka do you have [CustomTableInterceptor] outside of your partial class declaration? Also, maybe try to make your class public? Try running a debug session after that with your breakpoint set, and see what happens.

0 votesVote for this answer Mark as a Correct answer

Laura Frese answered on November 3, 2015 00:43

I did have [CustomTableInterceptor] outside of the partial class declaration but it didnt make a difference. I tried public as you suggested. That didnt work either. Thank you for your suggestions Joshua.

0 votesVote for this answer Mark as a Correct answer

Virgil Carroll answered on November 3, 2015 06:42

You need to register the custom class so Kentico recognizes it as something to run. Take a look at how it is done here: https://docs.kentico.com/display/K8/Handling+global+events

0 votesVote for this answer Mark as a Correct answer

Laura Frese answered on November 4, 2015 17:44

The event is registered. The instructions to register are to " use the CMSModuleLoader partial class in the App_Code folder" which is included in my code. Maybe there is something else I am missing?

I am able to get the above code to work when changes are made to the data from within the CMS > Custom tables > table name > data however when I update the data on a page using the custom table form web part it does not catch the event. I need the insert/update/delete events to be caught no matter where the data is modified from.

For now I have resolved this issue by cloning the custom table form web part (CMSWebParts/CustomTables/CustomTableForm.ascx) and then manipulating the data in the form_OnAfterSave method. Ideally I would catch all custom table events so if anybody else has any suggestions for making this work in App_Code ....

 protected override void OnLoad(EventArgs e)
{
    form.OnAfterSave += form_OnAfterSave;
    form.OnBeforeSave += form_OnBeforeSave;
    base.OnLoad(e);
}

  protected void form_OnAfterSave(object sender, EventArgs e)
{

}
0 votesVote for this answer Mark as a Correct answer

Drew Robison answered on November 4, 2015 20:30

You need to then use the CustomTableItem Class or CustomTableItemProvider class, the CustomTable class will be on the creation of the custom table, where you need to actually perform actions on when an item is inserted.

2 votesVote for this answer Mark as a Correct answer

Laura Frese answered on November 6, 2015 20:38 (last edited on November 6, 2015 20:43)

Thank you Brenden and Drew! I was able to get it to work using CustomTableItemEvents.Update.After , suggested by Martin Florian.

CustomTableInfo.TYPEINFO.Events.Update.After - use this when you want to change definition of the table – fields, name,…

Final result is

[CustomTableInterceptor]
public partial class CMSModuleLoader
{
    public class CustomTableInterceptor : CMSLoaderAttribute
    {
        public override void Init()
        {
             CustomTableItemEvents.Update.After += CustomTableItem_After_Update;
        }

        protected void CustomTableItem_After_Update(object sender, CustomTableItemEventArgs e)
        {
            CustomTableItem customtableitem = (CustomTableItem)e.Item;
            string tablename = customtableitem.ClassName;
            //etc
        }
    }
2 votesVote for this answer Mark as a Correct answer

Tom Troughton answered on November 30, 2015 14:02

Gah! Missing from documentation. Thanks for battling with this so I didn't have to ;)

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.