How to implement SpItemEventReciever class?

This article discusses how to implement event and event handlers using SPItemEventReceiver class.


Here are the steps:


Step 1.

Add a new class to your solution and inherit this new class from SPItemEventReceiver. After that, you must override event handlers in your class.


Step 2.

After that you must define the lists or items that your events will listen or monitor the events you need to catch and handle them.


To do so, you need to write two xml sheets that will define the scope you will monitor to catch your events. These two XML files are - a. Feature.xml , b. Element.xml


a. Feature.xml :

We define in it our dll that is produced from our class that will catch the events and implement them and refers to the element.xml sheet that minimizes the scope of monitoring.


b. Element.xml :

This file minimizes the scope of monitoring, without it we can catch for example all the itemupdate events fired in all the site in any list under any subsite, but practically this is not useful so we need to define our scope.


The following code illustrates Feature and Element xml sheets:


Feature.xml :


Id : refers to GUID id that can be generated from guidgen.exe placed at \program files\Microsoft Visual Studio 8 .

Feature Scope : here, our scope is all the site ( web ) .


We can remove the tags of to make our scope is the whole web.


Element.xml :


Now we will discuss the elements in between the receiver tags and discuss each of them

Name: we write here a name that describes our event handler.


Type: here we usually write the name of the event such as ItemAdded , ItemUpdated.

then define your assembly and your class as represented in the attached image.

ListTemplateId= for lists and document libraries we use id= 101 , but for Photo Galleries we use id=100


and to install the previous two files you can read it from http://msdn.microsoft.com/en-us/library/ms453149.aspx



Step 3.

After that build your project and place your DLLs in GAC at Local_Drive:\WINDOWS\assembly




By the way, you wont be able to debug your solution to allow debugging by default. If you wish to enable debugging, follow these steps.


1.Open Run and run the command prompt .

Run --> cmd


then write subst g: c:\windows\assembly


the previous code makes a virtual drive named (g:) and when you open it you will find some folders that extends the GAC .


2. Open GAC_MSIL you will find a folder with the same name of your dll and your dll is placed in it , and then place your .pdb file in that folder.



after all that you are ready to work with your event handler class.


Important Notes for Debugging:

when changing your code and you need to debug again there are some very easy steps so you could have a happy debugging :)

1. uninstall your dll from GAC. (uninstall dont replace it , some times it makes some weird actions so its better to uninstall it).

2. restart your iis by Run-->cmd-->iisreset.

3. place your dlls again into gac and put your .pdb file in g:\gac_msil \dllName.

4. attach your solution to the w3wp process running.

5 . have a happy debugging time :).



In my next post, I will put a project that adds a digital signature to files uploaded in a document library that will be done when the file is approved. This code will handle and show you lot of interaction with Sharepoint.