API
Version 7.x > API > Custom event handler for user creation and role assignment View modes: 
User avatar
Certified Developer 8
Certified Developer 8
matthew.mason-fcv - 8/2/2013 12:52:26 PM
   
Custom event handler for user creation and role assignment
Hi,

Essentially I'm trying to create a custom event handler for a project that will fire when a user is created/deleted in Kentico and when a role is assigned to that user.

I've been trying to follow the documentation: http://devnet.kentico.com/docs/devguide/index.html?event_handlers_overview.htm

But I have been unable to get it to work.

Here is the test handler I created:
using System;
using CMS.DocumentEngine;
using CMS.SettingsProvider;
using CMS.GlobalHelper;

[CustomUserEvents]
public partial class CMSModuleLoader
{
private class CustomUserEvents : CMSLoaderAttribute
{
public override void Init()
{
UserInfo.TYPEINFO.Events.Insert.Before += User_Inserted_Before;
}

private void User_Inserted_Before(object sender, ObjectEventArgs e)
{
string lines = "This works";

System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
file.WriteLine(lines);
}
}
}


Our project is a web application project so it doesn't have an App_Code folder by default. I've tried creating one, I've put the file in both our project and the associated CMSApp project containing the Kentico application.

Still can't get it to work? It doesn't seem to fire and if I debug I can't see it ever hitting the Init method or anything.

I saw this thread and read this blog post that was mentioned there.

Do you have any ideas or can you give me an example of how to do this?

Thanks!

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 8/3/2013 9:57:15 PM
   
RE:Custom event handler for user creation and role assignment
Hi,

Your code looks good. But, in the web application, please place the file to the Old_App_Code folder as described in the documentation. Also, make sure that the file is included to the web application project and since it is a web app. I would build the project first.

Best regards,
Juraj Ondrus

User avatar
Certified Developer 8
Certified Developer 8
matthew.mason-fcv - 8/6/2013 11:17:38 AM
   
RE:Custom event handler for user creation and role assignment
Thanks for the reply Juraj :)

I've tried placing the code in the Old_App_Code folder but it's still not working? No breakpoints are hit if I debug, nothing seems to fire.

I've tried it the old way, with the seperate project using a custom data handler which works fine, but since it's a deprecated method it's not ideal.

I don't suppose you have any more ideas?

Regards,

Matt.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 8/7/2013 12:51:03 AM
   
RE:Custom event handler for user creation and role assignment
Hi,

From your description is not clear whether the class file was included into the web application project and whether you have built it or not. Could you please clarify?

It is working just fine for me.

Best regards,
Juraj Ondrus

User avatar
Member
Member
Swainy - 8/7/2013 5:46:01 AM
   
RE:Custom event handler for user creation and role assignment
Hi Juraj,

I am doing exactly this in a web app project and it's working correctly for me too. I have just included my file in a folder called classes in the root (not in the old_appcode folder) and it works correctly on load and sets the events.

I have built the project and I am using a web app version 7.0

Thanks,

Matt

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 8/7/2013 8:16:41 AM
   
RE:Custom event handler for user creation and role assignment
Hello,

Thank you for the input. I just wanted to be sure that the class file is included into the project. Not all project files are included by default if you open the solution in Visual studio - sometimes you need to explicitly include given file to the project and build the solution.

Best regards,
Juraj Ondrus