Send Email Notification When User Settings Updated in Kentico

In a project I worked on recently, one of the specifications was to send out a notification email to the administrators when any user updated a user profile. This was to be done whether the administrators updated a profile through the Site Manager (this is a Kentico 7 site), or through a widget on the users’s dashboard. To get this to work, I had to hook into Kentico’s global event handlers to send the email notification after the settings have been updated.

I initially just hooked into the UserInfo.TYPEINFO.Update.After event in order to send out a notification email each time a user’s information was updated. With this method I was getting duplicate emails being sent out. I emailed Kentico support to find out more information.

It turns out that when user settings are saved, the event is triggered three times. Once for the user’s picture, once for the user settings object, and a third time for the user’s OpenID information. When Kentico loads the class, it loads one instance for the duration of the application. This means I can keep track of how many times the event has been triggered and abort before the email is actually being sent

The code below is a class which will be loaded by Kentico, hook into the UserInfo.TYPEINFO.Update.Before event to track how many times the After event will be fired, letting me know if I should continue with the notification email.

This code was written and tested for Kentico 7. I have not tested it for Kentico 8. Although I believe that the logic behind this class will work in Kentico 8, some of the code below will have to be upgraded to handle the API changes introduced in Kentico 8.
using System;
using CMS.DocumentEngine;
using CMS.SettingsProvider;
using CMS.SiteProvider;
using CMS.CMSHelper;
using System.Data;
using CMS.GlobalHelper;

/// <summary>

/// </summary>
[CustomUserEventHandler]
public partial class CMSModuleLoader
{
    /// <summary> 
    /// Attribute class that ensures the loading of custom handlers 
    /// </summary> 
    private class CustomUserEventHandler : CMSLoaderAttribute
    {
        int count = 0; // Holds the number of times the events have fired to prevent duplicate emails.

        /// <summary>
        /// Called automatically when the application starts
        /// </summary>
        public override void Init()
        {
            UserInfo.TYPEINFO.Events.Update.Before += Update_Before;
            UserInfo.TYPEINFO.Events.Update.After += Update_After;
        }

        void Update_Before(object sender, ObjectEventArgs e)
        {
            UserInfo user = e.Object as UserInfo; // Make sure we're dealing with a UserInfo object.
            if (user == null)
                return;

            count++;
        }

        void Update_After(object sender, ObjectEventArgs e)
        {            
            UserInfo user = e.Object as UserInfo; // Make sure we're dealing with a UserInfo object.
            if (user == null)
                return;

            if (count > 1) // If the object has been updated recently, we don't want to get out of the way
            {
                if (count == 2)
                    count = 0;

                return;
            }

            // Put your notification code here.
        }
    }
}

Written on October 3rd, 2014 by Dan Walker
Tags: kentico, how-to.

Want to read more?

If you’ve enjoyed what you’ve seen so far, you might like some of my other posts. Visit the archive to see all of my past writings.

Blog Archive »
 

About the Author

Dan Walker is a programmer from Grand Rapids, MI. He works at Gordon Food Service.

More About Dan »

Get in touch