What is the difference between Sitecore Pipelines and a Sitecore Event Handlers for a specific event? When should we customise a pipeline and when should we add a new handler?
To me pipelines are about process. Do this, then this. Allowing you to insert and remove processes simply with patch configs. A great example of this is
login user pipeline. Your pipeline would be:
- Log user in
- Identify user in xDB
- Add logging info for user
- Add user to marketing automation for returning customer
It is a process flow that you can reorder and add to/subtract from.
Events are one off, very focused actions. Like item:saved vs item:saving. item:renamed vs item:duplicated. They are extremely focused. Although you could tie many events together in the config, they are not very process oriented. It is more about data manipulation than process.
To add to @Chris Auer, think of it this way:
What is the difference
An event is a reaction to something happening in the system. This could be a user interaction, like
item:created, etc, or it could be raised by another process in the system like
publish:end:remote. And there can be many handlers for a single event, just look at some of the
item: events and how many handlers SXA adds to them.
The key point here is that an event reacts to something else being done in the platform.
A pipeline requires something to start that process, but as Chris mentioned, the pipeline is a pattern that starts with some data and then sends that data through various processors to consume, manipulate or add to that data. So you might think of
mvc.getPageItem pipeline; that pipeline takes the initial data about the page context and each processor does something with that data, from setting the language, trying different methods to resolve the page item to hooking into the Content Testing module.
The key for pipelines is that it's processing data.
When should I use event handlers/pipelines
Now we have the distinction, it should be fairly easy to know what you want to use.
If you want to react to an existing event that occurs in the system, you probably want to create an event handler. It is important to remember that hooking into some of the item event handlers can seriously affect your editor performance. You should make sure the code is as performant as you can get it. Don't take shortcuts just because it won't affect the delivery of the site.
As for pipelines, its unusual to create a completely custom pipeline, although it does happen. More often you are hooking into an existing pipeline and adding a processor there. So if you find that you need to change the way something in Sitecore functions, say for example you want to extend the
source property on a
TreeList, then you would be creating a pipeline processor to do that.