Data handler (CustomDataHandler class)

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

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

 

OnBeforeUpdate
OnAfterUpdate
OnBeforeInsert
OnAfterInsert
OnBeforeDelete
OnAfterDelete
OnGetContent

 

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.

 

Example

 

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:

 

[C#]

 

using CMS.DataEngine;

using CMS.GlobalHelper;

using CMS.EmailEngine;

 

2.Put the following code inside the OnAfterUpdate method.
 

[C#]

 

// 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 = "admin@domain.com";

// get the user's e-mail address

email.Recipients = ValidationHelper.GetString(dataItem.GetValue("Email"), "admin@domain.com");

email.Subject = "Your password";

// get the user's password

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

EmailSender.SendEmail(email);              

}

 

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.

 

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

 

4. If you don't have refference 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.

 

Page url: http://devnet.kentico.com/docs/5_5r2/devguide/index.html?data_handler.htm