8

I've been tasked with an interesting challenge. The client wants a user to be sent an email informing them when they have been assigned to a specific role by an Admin.

What will be the best way to go about this?

Is it possible to do this via Workflows? Or will other methods be required?

11

One option is to hook into Sitecore's user:updated event (and optionally user:updated:remote). This event is raised when a user has been updated, the remote event handler is raised when a user was updated on a remote Sitecore instance.

EDIT: As pointed out in the comments by @SzymonKuzniak, you can also hook into the roles:usersAdded[:remote] event to specifically handle added roles. Likewise, there is also the roles:rolesRemoved[:remote] event.

Create a new configuration file and put it in App_Config\Include:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event name="user:updated">
        <handler type="Custom.UserUpdatedEventHandler, Custom" method="OnUserUpdated"/>
      </event>
      <event name="user:updated:remote">
        <handler type="Custom.UserUpdatedEventHandler, Custom" method="OnUserUpdatedRemote"/>
      </event>
    </events>
  </sitecore>
</configuration>

Create a class UserUpdatedEventHandler in the Custom namespace:

public class UserUpdatedEventHandler
{
    public void OnUserUpdated(object o, EventArgs args)
    {
        var sitecoreArgs = (SitecoreEventArgs)args;
        var membershipUser = (MembershipUser)sitecoreArgs.Parameters[0];

        // Check user and roles

        // Send e-mail
    }

    public void OnUserUpdatedRemote(object o, EventArgs args)
    {
        var sitecoreArgs = (SitecoreEventArgs)args;
        var userRemoteEventArgs = (UserUpdatedRemoteEventArgs)sitecoreArgs.Parameters[0];
        var userName = userRemoteEventArgs.UserName;

        // Check user and roles

        // Send e-mail
    }
}
  • There's also roles:usersAdded which handles specifically the case of users being added to role. – Szymon Kuzniak Sep 16 '16 at 10:13
  • Good suggestion. After trying this I keep getting the following error at this point: var membershipUser = (MembershipUser)sitecoreArgs.Parameters[0]; "Unable to cast object of type 'System.String[]' to type 'System.Web.Security.MembershipUser'. – user337 Sep 22 '16 at 16:00
  • @user337 That is because the roles:usersAdded event has other arguments than the user:updated event. Probably - haven't tested this myself - the argument is an array of usernames that were added to the role. You need to inspect which parameters the sitecoreArgs has and act accordingly. I guess there must be a rolename somewhere too. – Thomas D Sep 23 '16 at 6:16
  • 1
    @user337 has this answered your question? If so please mark it as answered – Diego Apr 10 '17 at 15:14
  • @user1924455 - Please mark this as answered – Peter Procházka Mar 7 '18 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.