SharePoint 2010 has a very robust event handling capability that can be used for custom pre and post list event processing. When you create your custom event receivers, you are writing event handlers for different events that SharePoint fires due to an action. The main events triggered by SharePoint can be classified as follows:
The preceding events are triggered by SharePoint when changes, creations, or deletions happen on different objects like Lists, Sites, Site Collections, List Items, and so on.
All the events listed previously can be further classified as, Synchronous and Asynchronous events. Synchronous events are those that are fired before an action takes place (for example, the
ItemAdding event or the
ItemDeleting event on a List Item). Synchronous events are executed on the same thread as the code, before sending the response to the browser.
Asynchronous events are those that take place after the action has happened for example, the FeatureActivated event or FeatureInstalled event on Features.
For example, a Synchronous event
ItemAdded on list item can be used to verify the data that is being added to a list before it gets added. This way you have control over the data that gets added and if needed, you can cancel the data getting added to the list. You cannot cancel an Asynchronous event. Asynchronous events are used for business process flow like sending an e-mail after the item gets added to the list.
Typical scenarios that include creating event handlers are as follows:
Custom data validation so you can avoid adding an item to the list if data validation fails
Sending a custom e-mail when an item is added to a list
Logging to external database for audit purposes and so on
The custom event receiver you write will be packaged as a solution file (with .
wsp extension) to deploy to SharePoint event host. Every event receiver is bound to some SharePoint object which is also its host. Site, Web, List, Features, and so on are some of the examples of the hosts. In the previous versions of the Visual Studio, there were no out-of-the-box templates that supported SharePoint development. You had to manually create your manifest files and
feature.xml files and use
for creating your solution files to deploy. The other alternative was to use open source tools like WSPBuilder for making life a little easier. However, this is not the case with Visual Studio 2010. There are templates available for Event Receivers, Workflows, List Definitions, Visual Web Parts, and many more. We will work with many of them in the subsequent chapters.
Do not use event handlers for long running processes. Use Workflows for that purpose. We will handle Workflows in Chapter 2, Workflows.
Feature receivers like List Event Receivers are handlers that you write when certain events happen from Features. In the Feature Event Receivers, you can write custom handlers when events like Feature Installation, Feature Activation, Feature Deactivation, or Feature Uninstall happens.
Feature Event Receivers are used when you, as a programmer, needs to create entries in
web.config for database access or if you are a product vendor, then activate license files, and so on when a Feature is installed or activated. You can also use event receivers for clean-up activities like removing an entry from
web.config or delete a list, and so on when a Feature is removed or deactivated.