Tuesday, 9 December 2008

How to Create a Simple Event Handler when a Feature is activated

If you want to bind an event handler with a simple feature all you need to do is tweak your Feature.xml and add the Assembly Information into that and Create a Simple Event handler
Feature.xml File
Feature Id="ec4b9423-46ff-4a61-9264-8761e7c17145"
Description="Description for FeatureWithReceiver1"
ReceiverAssembly="SimpleEventHandlerPro, Version=, Culture=neutral, PublicKeyToken=$PublicKeyToken$"
Creating a Simple Event Handler
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SimpleEventHandlerPro
class FeatureWithReceiver1 : SPFeatureReceiver

public override void FeatureInstalled(SPFeatureReceiverProperties properties)

SPSite rootsite = new SPSite("http://js-upsideserver");
SPWeb mywebsite = rootsite.OpenWeb();
mywebsite.Title = "JJ SITE Changed";

foreach (SPList list in mywebsite.Lists)
list.EnableFolderCreation = false;



public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

public override void FeatureActivated (SPFeatureReceiverProperties properties)
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)


How to enable document library event handlers for the newly created document libraries at the list definition level

The List definition files can be configured in such a way that the event handler configured automatically for the newly created document libraries. Here I provide the details of how to do it.
Open the Site definition folder. If you use default Site definition, STS, then open the folder from the following location.
“C:\program files\common files\Microsoft Shared\web server extensions\60\template\1033\STS
Note: Modifying the default files is not supported by Microsoft. Microsoft recommends using custom Site Definition.
Open the List Definition folder. If you use default list definition for document library, then open the “DOCLIB” folder in the LISTS folder
Open the “SCHEMA.XML” file in notepad or visual studio
Modify the LIST node by adding the EventSingAssembly, EventSinkClass, and EventSinkData

Note:Here I hardcoded the EventSinkAssembly for sample. Could you please change it according to your component.
Restart IIS.
After configuring the details, if you create new document library based on the list definition, then the document library event handler will be automatically configured.

Event Handlers and Event Receiver’s in MOSS

Tere are two types of Events Synchronous and Asynchronous Events.* Synchronous events can trap an item, document library or site before it is deleted,added or updated.eg, ItemAdding, ItemUpdating,ItemDeleting etc.* Asynchronous events can trap an item, document library or site when it is added,deleted or updated.eg, ItemAdded, ItemUpdated,ItemDeleted etc. We create Event Recivers to handle these events. An event receiver basically, is a piece of managed code that is launched in response to an event that takes place within SharePoint.SharePoint Object Model exposes several event classes inherited 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.

How to bind SharePoint event receivers to a custom list definition

In most cases you want to create your own list definition with your own event receiver that sould only react on events of your custom list.
SharePoint ships with alot of list definitions with unique list type numbers:
100: Generic list101: Document library104: Announcements listand many more.
You can create event receivers that react on events of these list types.
To create a custom list definition you should use the visual studio template "List Definition" shipped with the VSeWSS 1.1. If you create a custom list with base type "document library" its id will also be 101. It's just a copy of the template you selected.

First line of schema.xml after creating the custom list definition project:

SharePoint Template Sites

  SharePoint look book Get inspired Discover the modern experiences you can build with SharePoint in Microsoft 365 https://lookbook.microsof...