SharePoint Event Handler is a program that enhances and adds functionality throughout SharePoint List, List Items or Sites. Event Feature can be deployed to new or existing sites by using Features. SharePoint Object Model provides several event classes that can target event handlers to List, List Item or Sites.
Synchronous and Asynchronous Events
In addition to asynchronous events, SharePoint Server 2007 introduces synchronous events i.e. events that activate before the action occurs. Synchronous events can trap an item, document library or site before it is deleted. Synchronous event removed the ability for users to delete an item from the document library or event restricts users to delete columns from the document library.
SharePoint Object Model exposes several event classes inherit from Microsoft.SharePoint assembly. There are three main event classes:
- SPItemEventReceiver
- SPListEventReceiver
- SPWebEventReceiver
Each class includes both synchronous and asynchronous methods to work with Item, List or Web Level. In this article, I will demonstrate how hook an ItemDeleting, FieldDeleting or SiteDeleting Event to restrict users from deleting an item from the document library or restrict users from deleting a column from the document library.
SPItemEventReceiver
By overriding a member of SPItemEventReceiver class, developers can restrict users from deleting, updating an Item in a List. Some of the important SPItemEventReceiver class members are described below.
|
Event
|
Event Type
|
Description
|
|
ItemAdded
|
Asynchronous
|
After an Item Added in a list
|
|
ItemAdding
|
Synchronous
|
Before an Item Added in a list
|
|
ItemAttachmentAdded
|
Asynchronous
|
After an attachment added from a list item
|
|
ItemAttachmentAdding
|
Synchronous
|
Before an attachment added from a list item
|
|
ItemAttachmentDeleted
|
Asynchronous
|
After attachment deleted from a list item
|
|
ItemAttachmentDeleting
|
Synchronous
|
Before attachment deleted from a list item
|
|
ItemCheckedOut
|
Asynchronous
|
After an item checked-In in a list
|
|
ItemCheckingOut
|
Synchronous
|
Before an item is checked out in a list
|
|
ItemCheckedIn
|
Asynchronous
|
After an item is checked-In in a list
|
|
ItemCheckingIn
|
Synchronous
|
Before an item is checked-In in a list
|
|
ItemDeleted
|
Asynchronous
|
After an item is deleted from a list
|
|
ItemDeleting
|
Synchronous
|
Before an item is deleted from a list
|
|
ItemUpdated
|
Asynchronous
|
After an item is updated in a list
|
|
ItemUpdating
|
Synchronous
|
Before an item is updated in a list
|
For the demonstration, I am using a synchronous event i.e. ItemDeleting, which restrict users of deleting an item in a list (document library). You can invoke this event with any type of list of document library by specifying the ListTemplateId attribute in a Feature Schema file e.g. ItemEventReceiver.xml.
See below screen shot of ItemEventReceiver.cs and ItemEventReceiver.xml for defining the ListTemplateId and Assembly.

ItemEventReceiver.cs class

ItemEventReceiver.xml file
The following table shows default list template integer IDs that you can use in defining the template ID for event handlers.
|
List Template Id
|
List Template
|
|
100
|
Generic list
|
|
101
|
Document library
|
|
102
|
Survey
|
|
103
|
Links list
|
|
104
|
Announcements list
|
|
105
|
Contacts list
|
|
106
|
Events list
|
|
107
|
Tasks list
|
|
108
|
Discussion board
|
|
109
|
Picture library
|
|
110
|
Data sources
|
|
111
|
Site template gallery
|
|
113
|
Web Part gallery
|
|
114
|
List template gallery
|
|
115
|
XML Form library
|
|
120
|
Custom grid for a list
|
|
200
|
Meeting Series list
|
|
201
|
Meeting Agenda list
|
|
202
|
Meeting Attendees list
|
|
204
|
Meeting Decisions list
|
|
207
|
Meeting Objectives list
|
|
210
|
Meeting text box
|
|
211
|
Meeting Things To Bring list
|
|
212
|
Meeting Workspace Pages list
|
|
300
|
Portal Sites list
|
|
1100
|
Issue tracking
|
|
2002
|
Personal document library
|
|
2003
|
Private document library
|
SPListEventReceiver
By overriding a member of SPListEventReceiver class, developers can restrict users from deleting, updating a list from a Site. Some of the important SPListEventReceiver class members are described below.
|
Event
|
Event Type
|
Description
|
|
FileAdded
|
Asynchronous
|
After a document is added in a List
(Document Library)
|
|
FileAdding
|
Synchronous
|
Before a document is added in a List
(Document Library)
|
|
FileDeleted
|
Asynchronous
|
After a document is deleted from a list
(Document Library)
|
|
FileDeleting
|
Synchronous
|
Before a document is deleted from a list
(Document Library)
|
|
FileUpdated
|
Asynchronous
|
After a document is updated in a list
(Document Library)
|
|
FileUpdating
|
Synchronous
|
Before a document is updated in a list
(Document Library)
|
For the demonstration, I am using a synchronous event i.e. FieldDeleting, which restrict users of deleting a document from a list (Document Library). In this case, I am again specifying ListTemplateId = 101 for Document Library in a ListEventReceiver.xml file.
SPWebEventReceiver
By overriding a member of SPWebEventReceiver class, developers can restrict users from deleting a site from a site collection or even completely delete a site collection. Some of the important SPWebEventReceiver class members are described below.
|
Event
|
Event Type
|
Description
|
|
SiteDeleted
|
Asynchronous
|
After a site collection has been deleted.
|
|
SiteDeleting
|
Synchronous
|
Before a site collection is being deleted.
|
|
WebDeleted
|
Asynchronous
|
After a web site has been deleted.
|
|
WebDeleting
|
Synchronous
|
Before a web site is being deleted.
|
For the demonstration, I am using a synchronous event i.e. WebDeleting, which restrict users of deleting a web site from a site collection.
We need Visual Studio 2005 to create a solution for providing the required functionality.
The steps required to create event handler for SharePoint Sever application are described below:
- Create a C#.NET Class Library Solution in Visual Studio .NET 2005 and give a name, such as EventHandlerFeature.
- On the Project Menu, choose Add Reference to Open Reference dialog box.
- Scroll down to Windows SharePoint Services and click the references the Microsoft.SharePoint.dll.
- Create a new folder inside Project Solution and give a name such as "Features".
- Create three folders inside Features folder and give names such as ItemEventReceiver, ListEventReceiver and WebEventReceiver folders.
- Create Feature.XML and feature schema file for ItemEventReceiver folder.
Attributes of Feature.XML File
Some of the attributes of Feature Tag are described below:
|
Attribute
|
Value
|
Description
|
|
ID
|
GUID
|
Contains the globally unique identifier (GUID) for the Feature.
|
|
Title
|
Text
|
Returns the title of the Feature. Limited to 255 characters.
|
|
Scope
|
Farm/WebApplication/Site/Web
|
Can contain one of the following values: Farm (farm), WebApplication (Web application), Site (site collection), and Web (Web site).
|
|
Hidden
|
True/False
|
This attribute equals FALSE by default.
|
|
AlwaysForceInstall
|
True/False
|
Optional Boolean. TRUE if the Feature is installed by force during installation even if the Feature is already installed. For example, if set to TRUE, Feature installation callouts will always fire anytime a user tries to install the Feature (even if it is already installed) by using either the scanforfeatures or installfeature command-line operation. This attribute equals FALSE by default. The AlwaysForceInstall attribute affects Features of all scopes.
|
See screen shot below of Feature.XML file.

- Repeat above step from ListEventReceiver and WebEventReceiver folders.
- Create a new C#.NET class and give a name, such as ItemEventReceiver.cs class. Add Microsoft.SharePoint namespace and inherits this class from SPItemEventReceiver class in order to overrides ItemDeleting member for restricting users from deleting an Item from a list or document library.
- Repeat above step and create two more classes and give names, such as ListEventReceiver.cs and WebEventReceiver.cs.
- Build the Solution
- Sign the solution by using SN utility of .NET command prompt or specifying the Sign Assembly name in the solution properties.
Note: Complete source code of solution and package file is attached with this article.