This post is the first post of 3 posts.
Post 1: Everything you need to know about MOSS Event Handlers
Post 2: Developer Solution Starter Kit (Building and deploying event handlers to MOSS)
Post 3: Registering Event Handlers plus Site Settings - Manage Event Handlers
Today I want to talk in depth about a feature of Microsoft Office SharePoint Server 2007 (MOSS) that shows you just how extensible the SharePoint 2007 platform is! That feature is the ability to add custom event handlers to sites, lists, items and content types in your portal.
What is a custom event handler? A custom event handler is a .Net assembly that contains the additional business logic you need to run when the event occurs in SharePoint.
Why did I pick this feature? Well, no portal or business application ever really lives in isolation to your other Line of Business (LOB) applications, and shouldn't! Your portal solution needs to be dynamic, expect change and work seamlessly with the other systems in your environment. So, you may ask, what does this have to do with Event Handlers?
Well, event handlers allow you to add [SOMETHING] your business needs into the functionality of SharePoint. [SOMETHING] examples:
o Retrieve information from a database such as filling in the remaining fields of a list based on a CustomerID (assuming you don't have an enterprise license for BDC)
o Call a web service
o Start a workflow
o Perform list data validation
o Convert document to PDF and store in alternative list.
o Access data in another list.
The possibilities around writing custom event handlers to perform an action based on an event in SharePoint 2007 are endless!
Here is a list of events that you can hook into:
· Microsoft.SharePoint.SPWebEventReceiver : "Site Level"
Occurs after a site collection has been deleted.
Occurs when a site collection is being deleted.
Asynchronous Afterevent that occurs after an existing Web site is completely deleted.
Synchronous before event that occurs before an existing Web site is completely deleted.
Asynchronous after event that occurs after an existing Web site has been moved.
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"
Occurs after a field link is added.
Occurs when a field link is being added to a content type.
Occurs after a field has been removed from the list.
Occurs when a field is in the process of being removed from the list.
Occurs after a field link has been updated
Occurs when a field link is being updated
· Microsoft.SharePoint.SPItemEventReceiver : "List Item Level"
Asynchronous After event that occurs after a new item has been added to its containing object.
Synchronous before event that occurs when a new item is added to its containing object.
Asynchronous after event that occurs after a user adds an attachment to an item.
Synchronous before event that occurs when a user adds an attachment to an item.
Asynchronous after event that occurs when after a user removes an attachment from an item.
Synchronous before event that occurs when a user removes an attachment from an item.
Asynchronous after event that occurs after an item is checked in.
Asynchronous after event that occurs after an item is checked out.
Synchronous before event that occurs as a file is being checked in.
Synchronous before event that occurs after an item is checked out.
Asynchronous after event that occurs after an existing item is completely deleted.
Synchronous before event that occurs before an existing item is completely deleted.
Occurs after a file is moved.
Occurs when a file is being moved.
Synchronous before event that occurs when an item is being unchecked out.
Asynchronous after event that occurs after an existing item is changed, for example, when the user changes data in one or more fields.
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?
· Occur before the event.
· Block the flow of code execution until your event handler completes.
· Provide you with the ability to cancel the events resulting in no after event (“...ed") being fired.
· Occur after the event.
· Do not block the flow of code execution in SharePoint.
Combining Custom Event Handlers with Content Types = POWER!
Content Type is just what it sounds like: a metadata description of content that can include custom properties, a retention policy, and an associated set of workflows and business processes.
For example, a company may define a “Contract” Content Type that includes the required metadata to make it easy to find that contract later.
The “Contract” Content Type is now a metadata description definition that can be associated to lists such as Document Libraries in your SharePoint environment. Once added, you now have the ability to add and manage Contracts added to your portal.
This is where Event Handlers come in: You can attach custom event handlers to a Content Type! Once attached to the Content Type definition, it picks up the events wherever you use the content type. This is extremely powerful! Why? Well, you have the ability to target individual content types across the entire site collection. For example, wherever a “Contract” Content Type is used, you could store a link in another system for each Contract created in your system, no matter what document library the contract is stored in.
When building custom event handlers, keep the following points in mind:
The assembly you deploy to the Global Assembly Cache(GAC) is running with full trust. Watch out for the following:
· Denial of Service attack: If a user uploads 10000 items to a list, what is the effect it will have to the systems your assembly uses.
· SQL Injection attack: If a user attempts to insert SQL statements into a column of a list that your assembly uses to update a database, what mechanisms are you using to make sure the input is valid.
· Cross Site scripting attack: What is the effect of adding script to a column that your assembly uses to update another area in SharePoint?
Watch out for the following:
· Load: What burden are you placing on your web front end servers to run the assembly each time an event occurs? Use performance counters to determine this.
· Long Running Operations: Consider creating a custom SharePoint timer job. Kick off/ Schedule the Timer Job from the event rather than running all the logic inside the event. This will allow you to use the features of SharePoint to view whether the Timer Job ran successfully.
· Sync vs Async Events: Synchronous events have to wait until your code completes before returning the page, whereas Asynchronous events show the page immediately.
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.
Limitations of Event Handler in SharePoint
Strangely there is no “SiteAdding”, “SiteAdded” event. However, if you really need this event you could create a “feature” to accomplish this. Features in SharePoint have a “FeatureActivated” event, where you could perform an action on creation of a site.
Custom Event handlers are a powerful tool in your arsenal as a SharePoint Architect/ Developer!