The CustomTreeNodeHandler class allows you to execute custom actions when a document (TreeNode) is created, updated or deleted. It's useful if you need to synchronize changes to external systems, generate off-line version of the document in PDF, etc.
It handles the following events:
• | OnBeforeInsert |
• | OnAfterInsert |
• | OnBeforeUpdate |
• | OnAfterUpdate |
• | OnBeforeDelete |
• | OnAfterDelete |
• | OnBeforeMove |
• | OnAfterMove |
• | OnBeforeCopy |
• | OnAfterCopy |
• | OnBeforeInsertNewCultureVersion |
• | OnAfterInsertNewCultureVersion |
Example
The following example shows how to handle the OnAfterInsert event and send the newly added news item by e-mail:
1. | Open the CustomTreeNodeHandler 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.TreeEngine; using CMS.GlobalHelper; using CMS.EmailEngine; |
2. | Put the following code inside the OnAfterInsert method. |
[C#]
// type the document as TreeNode TreeNode newsDoc = (TreeNode)treeNodeObj;
// handle the event only for news items if (newsDoc.NodeClassName.ToLower() == "cms.news") { // get content of the inserted news item and send it by e-mail EmailMessage email = new EmailMessage(); email.From = "admin@domain.com"; email.Recipients = "admin@domain.com"; email.Subject = ValidationHelper.GetString(newsDoc.GetValue("NewsTitle"), ""); email.Body = ValidationHelper.GetString(newsDoc.GetValue("NewsSummary"), ""); EmailSender.SendEmail(email); } |
3. | Set the From and Recipients e-mail addresses to you e-mail address. |
4. | Compile and run the project. Create a new document of type News. You should receive the e-mail message with it text. |
|
How to avoid neverending loops
If you need to call TreeNode.Update in the event handler (e.g. in the OnAfterUpdate event), you need to set TreeProvider.UseCustomHandlers property to false before calling the Update method. |
Page url: http://devnet.kentico.com/docs/5_5r2/devguide/index.html?treenode_handler.htm