New Event Binding Functionality in SharePoint Foundation 2010

Applies to: SharePoint Foundation 2010

Microsoft SharePoint Foundation 2010 expands the range and means by which you can bind events. Among the innovations is the ability to bind events at the scope of the site collection (SPSite), as well as giving you the ability to bind XML event receivers that are scoped to the individual site (SPWeb).

SPSite-Level Binding

To support enhanced functionality for list and site creation, SharePoint Foundation 2010 supports an SPSite-level event receivers collection. Like other event receiver collections, this one accepts bindings from SPWebEventReceiver, SPListEventReceiver, and SPItemEventReceiver objects.All of these event types can be scoped to the site collection level (that is, SPSite). However, an SPEmailEventReceiver object does not work at the site collection level because the way that this object is implemented.

XML Event Receiver Binding

SharePoint Foundation 2010 supports binding XML event receivers at the scopes of both SPWeb and SPSite instances by using the <Receivers> tag.

When a SharePoint Feature is scoped to the site collection level, a <Receivers> tag is potentially ambiguous. The tag can indicate either site-wide event binding or event binding for the top-level site. Note, however, that the default behavior is to scope to the site level.

Therefore, when you scope a Feature to the site collection level, use the attribute named Scope to specify the intended scope. When you set this attribute to "Site", event receivers that are defined inside a Feature that is scoped to the site collection are then bound at the site collection level. When the attribute is set to "Web", event receivers that are defined for a Feature of a site collection are bound at the root Web site of that site collection.

Event Binding by List Template

SharePoint Foundation 2010 supports event binding based on the SPItemEventReceiver event receiver by using semantics of the ListTemplateId property. This approach creates an event receiver definition that is scoped to the list as the list is created.

One practical result of this feature is that you can specify the binding in feature-based XML, as demonstrated in the following example.

<Receivers ListTemplateId="301">
   <Receiver>
      <Name>Blog post event handler for new trackbacks</Name>
      <Type>ItemAdded</Type>
      <SequenceNumber>1000</SequenceNumber>
      <Assembly>Trackback, Version=1.0.0.0, Culture=neutral,          PublicKeyToken=64e9de3efe88d9da</Assembly>
      <Class>Trackback.TrackbackEventsReceiver</Class>
      <Data>Property Logger</Data>
   </Receiver>
</Receivers>

The preceding XML binds a class named TrackbackEventsReceiver, which derives from the SPItemEventReceiverclass. TrackbackEventsReceiver implements the ItemAdded(SPItemEventProperties) method for all lists of template ID type "301" when they are created, or for existing lists when the respective feature is activated.

SharePoint Foundation 2010 also supports event binding that is based on the SPItemEventReceiver object in the XML of a specified content type.

See Also

Concepts

New Events in SharePoint Foundation 2010