Share via


Registering an Event Handler

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Event handlers are registered differently in Windows SharePoint Services 3.0 because of new concepts like content types and Features that it introduces. For backward compatibility reasons, however, Windows SharePoint Services 3.0 supports the existing registration of library events. The EventSinkAssembly, EventSinkClass, and the EventSinkData properties continue to function and are exposed in the user interface.

In Windows SharePoint Services 3.0, there are three fundamental ways to register an event handler:

  • Through the object model, as the SPWeb and SPList classes now each provide an EventReceivers property through which to access the collection of event receiver definitions for the Web site or list. You can add new event receivers by calling the Add method.

  • Declaratively by list type, for example, to register an event handler for all announcements lists. In a Feature.xml file, you can register an event handler by list template ID. When the containing feature is activated per SPWeb object, you can register the event handler for any list of the specified type.

  • Declaratively by content type, for example, to register an event handler for all documents of a specific type. Within the XML for a content type definition, you can register event receivers.

Note The assembly containing your event handler must be strongly named and registered in the global assembly cache (GAC) to be used. You cannot operate event receiver assemblies from the \_app_bin folder (Local_Drive:\Inetpub\wwwroot\wss\VirtualDirectories\GUID\_app_bin).

Content Types

Windows SharePoint Services 3.0 introduces the concept of content types in the data store. In short, content types introduce the notion of reusability to Web designers working with Windows SharePoint Services. Web designers now have the ability to 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 now activate SharePoint lists and libraries to support multiple content types. When you do that, you can attach one or more of these classes to your list or library and thus 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.

You can now define event handlers for a specific content type. You can, for example, define the content type "Customer", and within its behavior, you can define the metadata for the event handler.

Features

You define content types by using a feature. When you define a content type with a feature, you create two XML files as shown 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 storing all of the detailed information on the feature itself.

    <?xml version="1.0" encoding="utf-8"?>
    <Feature Scope="Web" 
      Title="Simple Event Handler Registration" 
      Id="A6B8687A-3200-4b01-AD76-09E8D163FB9A" 
      xmlns="http://schemas.microsoft.com/sharepoint/">
      <ElementManifests>
        <ElementManifest Location="elements.xml"/>
      </ElementManifests>
    </Feature>
    
  • 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 registers event receivers for deleting and adding 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=1.0.0.0, 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=1.0.0.0, Culture=neutral, PublicKeyToken=4358f2a5344ff0dc</Assembly>
          <Class>MyEventHandlers.SimpleEventHandler</Class>
          <Data></Data>
          <Filter></Filter>
        </Receiver>
      </Receivers>
    </Elements>
    

See Also

Tasks

How to: Create an Event Handler Feature

Concepts

Event Registrations

Creating a Basic Event Handler

Working with Features

Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio

Security Validation and Making Posts to Update Data

Elevation of Privilege

Other Resources

Content Types