Archive for December, 2007

Creating & Adding an Event Handler

In this document, we will create an event handler that captures the events of the document library and add an entry in the cell of the document added. We can add an event handler using code or as a feature. In order to write an event handler, we will first create a sharepoint site and create a document library. We will, then, capture the events of the document library.   


1         Writing an Event Handler

We will start from the Visual Studio 2005 new project window. In the new project window, select C# Window application, then, class library as encircled in the figure below and name the project “DocLibEventHandler”.


1.1    Adding Reference

The first thing we will do in the class library is to add reference of the name space Microsoft.SharePoint. This is done by right clicking on the References link in the solution explorer and then clicking on “Add Reference…” as encircled in the following figure:  


Clicking on add reference will generate a new pop up window Add Reference. We will select the Windows® SharePoint® Services namespace, as encircled in the figure below, and click on OK. It will add the Windows® SharePoint® Services namespace in our project.



We will add the names spaces, encircled red in the figure below, in our project so that we can directly use the classes in that namespace without specifying the complete path.



1.2   Changing Class Name

We will again go to the solution explorer and change the name our class from “Class1” to “DocLibEventHandlerClass”. This can be done by clicking on the class name in the solution explorer and right click on the class name. This will open a new window from which we will select Rename and change the class name from “Class1” to “DocLibEventHandlerClass”. As you change the name of the class, a prompt will appear, shown in the figure below, and ask would you like to rename all references to the code element “Class1”? Click on “Yes”


1.3   Inheriting Class

We will inherit our class from the class SPItemEventReceiver. The path to the class SPItemEventReceiver is Microsoft.SharePoint.SPItemeventReceiver. We have already added the namespace Microsoft.SharePoint, therefore, we just writet the name of the class SPItemEventReceiver for inheritance as shown in the figure below:    


1.4   Capturing Events

 MOSS provides two types of events namely, synchronous and asynchronous. The “…ing” event occurs before the action starts and the “…ed” occurs after the actions ends. “…ing” events occur synchronously while the “…ed” events occur asynchronously.   


Synchronous events

  • Occur before the event.
  • Block the flow of code execution until your event handler completes.
  • Provide you with the ability to cancel the events resulting in no after event (“…ed”) being fired.

Asynchronous events:

  • Occur after the event.
  • Do not block the flow of code execution in SharePoint

 To capture these events, class SPItemEventReceiver provides different methods, as shown in the right side of the above figure, which can be seen through Object Browser by pressing Alt+Ctrl+j to see the list of the methods by browsing through Microsoft.SharePoint to SPItemEventReceiver.  We will create a simple program for demonstration purpose. The code will override methods ItemAdding, ItemAdded, ItemUpdated and ItemUpdating in the class SPItemEventReceiver.  

1.5   The Code

 The code will run when a new file is added in the document library and also when an existing document is updated. It will add an entry in the list “CEO Docs Access Log” describing the nature of the event and date on which event occurred. The code is as under: 

namespace DocLibEventHandler


public class DocLibEventHandlerClass : SPItemEventReceiver   


public override void ItemAdded(SPItemEventProperties properties)       


SPListItem doc = properties.ListItem;

doc[“Comments”] = “Document has been added”;



//you can use following methods as well        

public override void ItemAdding(SPItemEventProperties properties)        {        }        

public override void ItemUpdated(SPItemEventProperties properties)        {        }        

public override void ItemUpdating(SPItemEventProperties properties)        {        } 


1.6   Signing the Assembly

 The next step is of signing the assembly. To do this, you have to move to the properties window of the project. To do this, right click on the project name “DocLibEventHandler” in the solution explorer and click on the properties in the popup menu. This will open the properties window, select the Signing Pane of the properties window and check the “Sign the Assembly” option. Select <New…> from the “Choose a strong name key file:” and give the name that you desired. In our case, we named it “DocLibeventAssembly” as shown in the figure below:  


1.6   Build Project

 We will build by pressing F6 or clicking on Build in the tolls menu and then clicking on Build Solution. Build will generate the file “DocLibEventHandler.dll” in the debug directory.  

1.7   Copying into GAC

 The file DocLibeventHandler.dll can be found in the debug folder whose path is C:\…..\Visual Studio 2005\DocLibEventHandler\DocLibEventHandler\bin\Debug. Depending upon your installation, you can find the file in the debug folder. Copy the file “DocLibeventHandler.dll” from this folder and paste it in the GAC folder which is normally found at C:\WINDOWS\assembly. After copying the file in the GAC, copy the public token key as encircled in the figure below:  


There are many other ways of adding assembly in GAC. 

1.8   Registering the Event Handler

 In order to register the event handler, add a new console project in the solution by right clicking on the solution “DocLibEventHandler” in the solution explorer as shown in the figure below:  


Select a console project in the “Add New Project” window and name it “DocLibRegApp” as shown in the figure below:  


 Add the following code in the main method. 

namespace DocLinRegApp{   

class Program    {       

static void Main(string[] args)        {           

SPSite sp = new SPSite(http://servername&#8221;);           

SPWeb website = sp.OpenWeb();           

SPList DocLib = website.Lists[“DLOne”];            

string assm = “DocLibEventHandler, Version=, Culture=neutral, PublicKeyToken=1d6b41a3ed9a92cb”;           

string class = “DocLibEventHandler.DocLibEventHandlerClass”;            

DocLib.EventReceivers.Add(SPEventReceiverType.ItemAdded, assm, class);           

DocLib.EventReceivers.Add(SPEventReceiverType.ItemAdding, assm, class);           

DocLib.EventReceivers.Add(SPEventReceiverType.ItemUpdating, assm, class);            DocLib.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assm, class);       




The string assm describes the assembly details that we have added in the GAC and class is the name of our class. Press F5 to run the project. 

1.9   Changes in the Document Library

Open the site and add document in the document library. Comments column will have the value “Document has been added” as shown in the figure below:


Comments (78)