Experiences of an Inland Empire Dad

SharePoint 2007–Event Handlers

SharePoint isn’t just an application.  It is an extensible application platform.  Easily extended with features that allow you to add custom event handlers to sites, lists, items and content types in your portal. A custom event handler is a .Net assembly that contains the additional business logic you need to run when the event occurs in SharePoint.

Examples of custom event handlers can include things like:

Event Types

Microsoft.SharePoint.SPWebEventReceiver : "Site Level"

 

SiteDeleted

Occurs after a site collection has been deleted.

SiteDeleting

Occurs when a site collection is being deleted.

WebDeleted

Asynchronous Afterevent that occurs after an existing Web site is completely deleted.

WebDeleting

Synchronous before event that occurs before an existing Web site is completely deleted.

WebMoved

Asynchronous after event that occurs after an existing Web site has been moved.

WebMoving

Synchronous before event that occurs before an existing Web site has been renamed or moved to a different parent object.

 

Microsoft.SharePoint.SPListEventReceiver : "List Level"

 

FieldAdded

Occurs after a field link is added.

FieldAdding

Occurs when a field link is being added to a content type.

FieldDeleted

Occurs after a field has been removed from the list.

FieldDeleting

Occurs when a field is in the process of being removed from the list.

FieldUpdated

Occurs after a field link has been updated

FieldUpdating

Occurs when a field link is being updated

 

Microsoft.SharePoint.SPItemEventReceiver : "List Item Level"

ItemAdded

Asynchronous After event that occurs after a new item has been added to its containing object.

ItemAdding

Synchronous before event that occurs when a new item is added to its containing object.

ItemAttachmentAdded

Asynchronous after event that occurs after a user adds an attachment to an item.

ItemAttachmentAdding

Synchronous before event that occurs when a user adds an attachment to an item.

ItemAttachmentDeleted

Asynchronous after event that occurs when after a user removes an attachment from an item.

ItemAttachmentDeleting

Synchronous before event that occurs when a user removes an attachment from an item.

ItemCheckedIn

Asynchronous after event that occurs after an item is checked in.

ItemCheckedOut

Asynchronous after event that occurs after an item is checked out.

ItemCheckingIn

Synchronous before event that occurs as a file is being checked in.

ItemCheckingOut

Synchronous before event that occurs after an item is checked out.

ItemDeleted

Asynchronous after event that occurs after an existing item is completely deleted.

ItemDeleting

Synchronous before event that occurs before an existing item is completely deleted.

ItemFileConverted

ItemFileMoved

Occurs after a file is moved.

ItemFileMoving

Occurs when a file is being moved.

ItemUncheckedOut

Synchronous before event that occurs when an item is being unchecked out.

ItemUncheckingOut

Synchronous before event that occurs when an item is being unchecked out.

ItemUpdated

Asynchronous after event that occurs after an existing item is changed, for example, when the user changes data in one or more fields.

ItemUpdating

Synchronous before event that occurs when an existing item is changed, for example, when the user changes data in one or more fields.

 

Asynchronous vs Synchronous Events: The "ing" and the "ed"

As you may have noticed above, there are two events raised for each type of event. The "…ing" event occurs before the action starts and the "…ed" occurs after the actions ends. "…ing" events occur synchronously while the "…ed" events occur asynchronously. What does this mean?

Synchronous events:
Asynchronous events:

So what are some of the Best PRactices when building custom event handlers?  Here are a couple of things you want to keep in mind:

1. Security:

The assembly you deploy to the Global Assembly Cache(GAC) is running with full trust. Watch out for the following:

2. Performance:

Watch out for the following:

3. Error Handling:

When using synchronous events and you decide to cancel an event, let the user know why the event was cancelled. For example, update a Task List with a message why it failed.  Log your errors so that you can determine what issue is occurring when your system is in production environment.

4. Connections: Cater for external systems being down when you are trying to connect to them. Don’t let your code / solution go belly up because you cannot connect to a database.

5. Bulk Operations:

The MSDN documentation mentions that event handlers will not fire when bulk operation is occurring. For example, when a new list is created, the FieldAdding event will not fire.