Kentico CMS 7.0 Developer's Guide

Data handler (CustomDataHandler class)

Data handler (CustomDataHandler class)

Previous topic Next topic Mail us feedback on this topic!  

Data handler (CustomDataHandler class)

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

This handler allows you to add custom actions to the following events:










The events are applied to all data items that are stored to the database. It includes documents, user information or any other settings. They receive the data object of type DataClass that can be modified. In case of delete handlers, the class name and ID is provided.




Handling document events


For handling document events, please use the CustomTreeNodeHandler class instead of the CustomDataHandler class. Every document uses up to 3 tables, which leads to three separate events in the CustomDataHandler class.




The following example shows how to handle the OnAfterUpdate event and send the password to the user whenever their profile is updated:


1.Open the CustomDataHandler class and put the following code at the beginning of the file. It adds the reference to the namespaces we will use to handle the event:




using CMS.DataEngine;

using CMS.GlobalHelper;

using CMS.EmailEngine;


2.Put the following code inside the OnAfterUpdate method.



// type the data object as DataClass

IDataClass dataItem = (IDataClass)dataObj;


// we want to handle only updates of user objects

if (dataItem.ClassName.ToLower() == "cms.user")


// we will use the CMS.EmailProvider to send e-mails

EmailMessage email = new EmailMessage();

email.From = "";

// get the user's e-mail address

email.Recipients = ValidationHelper.GetString(dataItem.GetValue("Email"), "");

email.Subject = "Your password";

// get the user's password

email.Body = "Your password is:" + ValidationHelper.GetString(dataItem.GetValue("UserPassword"), "");              




Please note: getting user's password in the example above is only possible when passwords are stored in Plain text format; the setting is located in Site Manager -> Settings -> Security & Membership.


3.Set the From and Recipients e-mail addresses to your e-mail address.


4. If you don't have reference to CMS.EmailEngine in your CustomEventHandler, add it there.

5. Compile and run the project. Edit some user profile that uses your e-mail address. You should receive the e-mail message with your password.