17.2 Working with Events 533
Chapter 17
17.2 Working with Events
So now that we understand the type of event handlers that can be written,
let’s look at the actual process involved in creating and implementing an
event handler. The process in and of itself is quite straightforward and
involves three main stages: creation, deployment, and registration. The cre-
ation stage typically involves a developer creating an event handler assembly
that overrides the relevant event receiver methods. Once the assembly file has
been created, the developer can then package the assembly into a solution file
for the administrator to deploy on the appropriate Web servers. Once the
event handler is installed all that remains is for the event receivers to be regis-
tered with the appropriate SharePoint containers, such as a specific Web site
or a set of document libraries. To make the administrator’s life a lot easier,
and make the deployment process more streamlined, the developer can
include the registration process as part of the solution package.
The following section will take you through the process of implement-
ing a simple event handler that prevents items from being deleted from a
specific document library within a specific SharePoint Web site, regardless
of the user’s permission level.
17.2.1 Creating an Event Handler
The first step in the process is to create the event handler. The event handler
needs to be able to trap when a user attempts to delete a Web site within our
site collection; therefore, our event handler will need to inherit the
SPItemEventReceiver class and override the ItemDeleting method. To
follow along with the example, create a new C# Class Library project in
Visual Studio 2005, add a reference to
Microsoft.SharePoint, and add
the code listed in Figure 17.14. Once the code is entered, you can build the
project assembly.
Figure 17.14
ItemDeleting code.
534 17.2 Working with Events
SharePoint event handler assemblies must be strongly named and
deployed to the global assembly cache (GAC) before they can be used. As a
quick refresh, you can use the Strong Name Tool (sn.exe) to strongly name
your assembly or, alternatively, use the Signing tab available in Visual Studio
2005. If you are using the Signing tab, then your assembly is strongly named
as part of the build process.
At this stage the assembly file has been created; next we need to deploy
the file and register the event handler.
17.2.2 Deploying an Event Handler
Once again, event handler assemblies must be strongly named and
deployed to the GAC before they can be used. A very simple way to deploy
an assembly file to the GAC is to drag the strongly named .dll build file
from its project directory and drop it into the GAC, which is usually
located in the
\WINDOWS\assembly directory. This particular method of
deployment is fine for demonstration purposes, but when it comes to pro-
duction, the recommended method of deploying event handlers is to lever-
age the Solution Deployment framework. Doing so allows you to inherit
all the benefits of the framework, such as providing a central place for
deployment to all appropriate Web applications and Web front-end servers,
providing a central place for upgrades and for retracting the solution, and
being able to package the deployment and registration of the event handler.
Chapter 11, Introduction to SharePoint Development, explains the steps
involved in packaging and deploying a Web Part solution; since an event
handler is just a .dll, the same technique can the applied to deploy the
event handler to the GAC of all the appropriate Web front-end servers.
As previously mentioned, the registration of the event handler can be
incorporated into the deployment process by including a registration
mechanism in the solution deployment files. For example, the solution
deployment package could include a Feature that, upon activation, would
register the event handlers to all the relevant SharePoint containers; upon
deactivation, the same Feature would unregister the event handlers. More
information on how to create Features that handle activation events can be
found in Chapter 16, Features.
17.2.3 Registering an Event Handler
The registration of an event handler refers to the process that takes a specific
method that a custom event handler overrides and binds it to a SharePoint
object such as a list, document library, or even a content type. For example,
in our scenario we can bind the
ItemDeleting method from our assembly to
a specific SharePoint list. If you examine the object model you will see that

Get Microsoft SharePoint 2007 Technologies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.