In Sitecore, is there a way that I can run custom logic when a user is added to a particular role?
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.
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.
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.
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.
<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>