7

In Sitecore, is there a way that I can run custom logic when a user is added to a particular role?

6

You can do this using Sitecore's eventing engine, by creating event handlers for the roles:usersAdded event, the roles:usersAdded:remote event, or both.

The roles:usersAdded event is raised when users have been added to one or more roles on the environment. In other words, if you have a single-server environment then this is the event that you will want to tie into. Additionally, if you have a split CM/CD environment and you want to listen for users being added to roles on the same server that you added the handler on then this is the event you will want to use.

In contrast, the roles:usersAdded:remote event is used on the CM server when you have a split CM\CD environment and want to listen for this event occurring on the CD.

While it may seem a little confusing that you have a :remote event for adding users to roles, when roles are stored in the Core DB which is shared by both the CM and CD, the events are fired from the code, so having a :remote listens for the code on the CD environment that adds users to roles.

| improve this answer | |
  • Sitecore PowerShell Extensions has an integration point where you can write a script to execute for the event. – Michael West Oct 11 '16 at 15:17
  • Ooh! That's pretty handy @MichaelWest! I didn't know that – Zachary Kniebel Oct 11 '16 at 15:45
4

Thought I would give a more complete answer.

If you would like to use Sitecore PowerShell Extensions to run custom scripts for events you can make use of the Events integration point.

There is an example out of the box that you can check out. You'll need to enable the module to make use of it.

Events Module

There is also a configuration file distributed with many events disabled by default. Check out Cognifide.PowerShell.Events.config.disabled for an example of what to patch.

Below is an untested example patch.

SPE.Roles.config

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event name="roles:usersAdded">
        <handler type="Sitecore.SharedSource.PowerShell.ScriptedItemEventHandler, Sitecore.SharedSource" method="OnEvent" />
      </event>
    </events>
  </sitecore>
</configuration>
| improve this answer | |
  • I will have to give this a try on my next implementation! Thanks, Michael! – Zachary Kniebel Oct 11 '16 at 16:15

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.