Binding Event Handlers by Using SharePoint Features.xml
Last modified: April 09, 2010
Applies to: SharePoint Foundation 2010
You can define event handlers for a specific content type in Microsoft SharePoint Foundation 2010 by using a SharePoint Feature. You can, for example, define the content type called "Customer", and in the definition of the customer behavior, you can define the metadata for the associated event handler.
You define content types by using a SharePoint Feature. When you define a content type with a feature, you create two XML files, as described below:
Feature.xml You use this XML file to define the metadata for the new feature. The following example code scopes the feature at the level of the site and defines a unique identifier for the new feature. Using the ElementManifests element, it then points to the location of the second XML file that stores all of the detailed information about the feature itself.
Elements.xml You use this file to define the assembly that encapsulates the event handler, the class itself, and also a sequence number that specifies the order, if multiple event handlers are associated with the Feature. The following example shows how to bind an event receiver that responds to the events for deleting and adding list items.
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Receivers ListTemplateId="104"> <Receiver> <Name>MyEventHandlers</Name> <Type>ItemDeleting</Type> <SequenceNumber>10000</SequenceNumber> <Assembly>MyEventHandlers, Version=220.127.116.11, Culture=neutral, PublicKeyToken=4358f2a5344ff0dc</Assembly> <Class>MyEventHandlers.SimpleEventHandler</Class> <Data></Data> <Filter></Filter> </Receiver> <Receiver> <Name>MyEventHandlers</Name> <Type>ItemAdded</Type> <SequenceNumber>10000</SequenceNumber> <Assembly>MyEventHandlers, Version=18.104.22.168, Culture=neutral, PublicKeyToken=4358f2a5344ff0dc</Assembly> <Class>MyEventHandlers.SimpleEventHandler</Class> <Data></Data> <Filter></Filter> </Receiver> </Receivers> </Elements>
Content types are reusable building blocks within the SharePoint Foundation data store. Frequently, a content type has one or more event receivers as part of its XML definition. In cases where you bind a content type to a target list, for example, then the event receivers that are contained in that content type are registered on (or bound to) that target list.
The vocabulary of events and event receivers sometimes uses the terms "to register" and "to bind" interchangeably. While this documentation speaks of "binding" event receivers, you may occasionally encounter references to "registering" event receivers. Both terms mean the same thing.
Windows SharePoint Services 3.0 introduced the concept of content types in the data store. In short, content types introduce the notion of reusability. By using content types, you can create classes of objects with specific definitions and possible associated behavior, such as type name, fields, format, business processes, retention, auditing, and event handling.
You can also activate SharePoint lists and libraries to support multiple content types. When doing so, you can attach one or more of these classes to your list or library and, in this way, extend it with additional functionality and behavior. Think of extending a customer list with a Contact content type. The Contact content type can provide the Customer list with a set of new fields, like Contact Name, Function, Telephone, and so forth, and also with new behavior.
If you need to bind an event handler to a specific list or library, or for a specific set of lists or libraries, you typically do this at the Feature level. However, you can also do this by using the EventReceivers property on the appropriate object itself (for example, on content type or list objects, by using EventReceivers, EventReceivers respectively).
With regard to lists and libraries, Features are an atomic SharePoint Foundation concept that represents specific Collaborative Application Markup Language (CAML) sections that were previously all consolidated within one file (Schema.xml or Onet.xml). These CAML sections are isolated, which means that you can reuse them in different places. You now create the structure and the field definition of a list by using a SharePoint Feature.
With regard to binding your event handler, you can use the concept of the Feature to bind your assembly to one specific list or library (by specifying the GUID of the list or library), or for a certain type of list or library (such as all document libraries or all form libraries). You define a Feature by creating the same two types of XML files (Feature.xml and Element.xml), as described in Binding a SharePoint Foundation Event Handler.