Binding a SharePoint Foundation Event Handler
Last modified: April 08, 2010
Applies to: SharePoint Foundation 2010
You must associate your event receiver with a SharePoint object type (the event host), which you do by binding the event receiver to the receiver host. There are two ways to bind an event receiver to a receiver host in Microsoft SharePoint Foundation 2010:
By using the SharePoint Foundation object model. (See Binding an Event Handler by Using the SharePoint Object Model.)
Most of the primary SharePoint object types like SPWeb and SPList provide an EventReceivers property that allows the object to serve as an event host. The event receivers for each event host are contained in a collection, the SPEventReceiverDefinitionCollection, which contains all of the individual event definitions (SPEventReceiverDefinition) that are associated with the host.
You can also bind events to event hosts declaratively by using the Feature.xml file. (See Binding Event Handlers by Using SharePoint Features.xml.)
Using the Feature.xml approach allows you to create broad reaching event bindings and then deploy them easily as part of a solution package. You can bind an event handler for all announcements lists, for example, by using the list template identifier (ID); then, when the containing feature is activated, you can bind the event handler for any list of the specified type.
Alternatively, you can bind events declaratively by content type – for example, to bind an event handler for all documents of a specified type. Note that you can bind event receivers by using the XML for a given content type definition.
For backward compatibility SharePoint Foundation supports the registration of library events. The EventSinkAssembly, the EventSinkClass, and the EventSinkData property continue to behave as in previous versions of SharePoint Foundation.
For a discussion of improvements to the event model that have been introduced in SharePoint Foundation 2010, see Event Model Improvements for SharePoint Foundation 2010.