Handling form events, Cannot be triggered by events that insert into Form.

Jay Ngo asked on April 21, 2020 05:40

Hi, Please someone please help me


  • I am using kentico 12 - MVC
  • I want to trigger the event insert into form
    • I followed this docs but it didn't work
    • https://docs.kentico.com/k12/custom-development/working-with-form-data-using-the-api#WorkingwithformdatausingtheAPI-Handlingformevents
    • It run the Init function, but FormItem_InsertAfterHandler doesn't actually run
    • note: works well with document when I change BizFormItemEvents to DocumentEvents
    • MyCode

[assembly: RegisterModule(typeof(FormsEventHandler))] namespace NTUCIncome.GlobalEventHandler {

public class FormsEventHandler : CMS.DataEngine.Module
{

    public FormsEventHandler() : base("FormsEventHandler")
    {
    }

    protected override void OnInit()
    {
        base.OnInit();
        BizFormItemEvents.Insert.After += FormItem_InsertAfterHandler;
    }

    private void FormItem_InsertAfterHandler(object sender, BizFormItemEventArgs e)
    {
        //logic
        var isCheck = true;
    }
}

}

Recent Answers


Juraj Ondrus answered on April 21, 2020 07:31

Where is the code/assembly placed? Is it on the Kentico admin app or also in the MVC app?

How and where are you testing it? Are you adding form entries through the live site (MVC app) or through the Recorded data tab in the admin UI (Kentico app)?

When handling global events in MVC, you may need to deploy the assembly containing the event registration code and handler methods to the separate MVC application (in addition to the Kentico administration project).

This is required if you are customizing any functionality that occurs on the live site. For example, if you have a custom handler for the Insert event of user objects, the event is raised both when creating a new user in the administration interface (Kentico application) and when a user registers on the live site (MVC application). For additional information, see Customizing MVC projects.

0 votesVote for this answer Mark as a Correct answer

Jay Ngo answered on April 21, 2020 07:59

Hi @Juraj Ondrus,

I placed the code above in Kentico admin app, under folder Old_App_Code, I created a new folder with name "GlobalEventHandler" then placed the code above in FormsEventHandler.cs file.

I tested in both ways, pulling to the live site in MVC app and added new records in Recorded data in CMS admin, but there is no luck. I tried to add "DocumentEvents.Insert.After" after "BizFormItemEvents.Insert.After". I can debug and delegated added, but only method for DocumentEvents can be trigger only.

Regards, Jay.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on April 21, 2020 08:39

Could you please try creating a new Class library project instead? As this way it is recommended. The Old_App_Code approach is more for portal engine and is getting outdated anyway.
I have created a custom assembly project in the WebApp.sln project (CMSApp) like on this screen shot. Added all the references and all the steps mentioned in the documentation and I also added the same project to the MVC app (screen shot) it is working fine - when submitting from live site as well as from admin.
As you can see in this screen shot the code is executed.

0 votesVote for this answer Mark as a Correct answer

Jay Ngo answered on April 21, 2020 09:09

Hi @Juraj Ondrus,

Thank for you suggestion. I will try and get back to you soon.

Jay

0 votesVote for this answer Mark as a Correct answer

Jay Ngo answered on April 22, 2020 06:52 (last edited on April 22, 2020 06:53)

Hi @Juraj Ondrus,


Thank you, I followed you but failed

  1. Create new library Project follow Create library project
    • Kentico.Libraries package
    • Edit the Custom project's AssemblyInfo.cs
    • Reference the Custom project from CMS project
  2. Debug into init but not trigger event insert into Form

Image Text

Do you have any suggestions for me?


Thank you so much

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on April 22, 2020 07:12

There are no build errors? Is the custom project DLL created in the BIN folder of Kentico app? From the screen shot I can see you created the project on the Kentico admin side - so are you entering the records through the Admin UI?

0 votesVote for this answer Mark as a Correct answer

Jay Ngo answered on April 22, 2020 07:22

  • Yes it has no build errors,
  • I reference from CMSApp project please ref image:

Image Text

  • I have tried inserting a record Form on the admin side Image Text

am i wrong? Thanks

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on April 22, 2020 07:37

I would try not using the CMSApp prefix - just to be sure there is no clash. Also, what type of custom assembly project you have selected? It should be "Class Library (.Net Framework)". Then, the screen shot shows that the DLL is created in the MVC app, next screen shot is from Kentico admin - so, you should be testing it from live site or make sure the DLL is created also in the Kentico admin application and its BIN folder.

0 votesVote for this answer Mark as a Correct answer

Jay Ngo answered on April 22, 2020 10:13

Thank you for quick reply @Jusraj Ondrus

I create lib project with Class Library (.Net Framework) (4.6.1). Removed project with CMSApp prefix. Create new lib project with ABC prefix. but still unsuccessful :(

Maybe you misunderstood: My KenticoStructureMVC name above is the solution name, I am trying to trigger the insert into Form at code of CMSApp project, not MVC project,

0 votesVote for this answer Mark as a Correct answer

Jay Ngo answered on April 22, 2020 11:47

but for DocumentEvents it works well, Could you give me another suggestion?

Image Text have a nice day bro

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on April 22, 2020 11:57

So, what are the steps how are you testing it? The custom project and module registration seems to be fine if other events are fired. So, maybe the issue is in the way how are you testing it.

0 votesVote for this answer Mark as a Correct answer

Jay Ngo answered on April 22, 2020 12:10

I tested by insert/edit for Form/ Page on Admin UI

note: I also tried inserting the Form into the page on the Page tab

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on April 22, 2020 12:18

Please, provide more details - how did you edited the form? Inserting form on the Page tab will not fire the event. The event is listening to entering data to the form - so you need to fill out the fields and submit the form.

0 votesVote for this answer Mark as a Correct answer

Jay Ngo answered on April 22, 2020 12:31

So sorry, This is how I test Form and Page

set debug in CustomBizFormHandler: Module [ FormItem_InsertAfterHandler, Document_Update_After]

Image Text

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on April 22, 2020 13:37

I am sorry but I am running out of ideas. I must be missing some small detail. If the page events are fired I do not see any reason why the form are not. I just tried it on a new project I have and it is working fine for me. This is my code - screen shot - and the same project is used in Kentico admin app as well as in the MVC app.
And in the Event log I see the entries just fine - one for Kentico admin (made by the administrator user) and also for MVC app (pulic user): screen shot

I would try cleaning the solution in Visual studio, make sure correct .Net version is used - the same as in the Kentico and MVC projects and that the "CustomBizformHandler" base name is not used in any other projects or event handlers. Or, maybe starting from scratch.

0 votesVote for this answer Mark as a Correct answer

Jay Ngo answered on April 23, 2020 04:30

Well, thank you very much for all your help. Juraj Ondrus

I will try again after clearup the visual studio

0 votesVote for this answer Mark as a Correct answer

Evgeny Petrov answered on December 2, 2020 23:44

Hi @Jay, I'm having the same issue, have you been able to find a solution/workaround?

0 votesVote for this answer Mark as a Correct answer

Laura Frese answered on February 2, 2021 17:59 (last edited on February 3, 2021 18:56)

The BizFormItemEvent is triggered via the MVC app, not the CMS, regardless of whether you insert the form via the Forms app in the CMS, or from a live site page. Therefore, this event handler code must be in the MVC App either directly or as an assembly.

In addition, if you are debugging and your assembly code was created in the project for the CMSApp and that project is attached to the CMS process, then your breakpoint wont be hit. You have to debug your BizFormItemEvent handler while the project the assembly is in is attached to the MVC process

0 votesVote for this answer Mark as a Correct answer

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