What is Event Handler in sharePoint? And how to work with it ?


Event handler as name suggest occurs when ever some event happen in your SharePoint site. Event handler can occur at different scope i.e. at site level, web level, list or library level. For this you need to write few code and you need to use two xml files. Later you need to activate it into your SharePoint site.

Event handler is very useful in SharePoint site. Suppose you want to throw a message or a specific event when any user complete some action.
In this condition event handle is useful. For example, you can use event handler whenever any user wants to delete an item from the list, you can create an event handler when any user wants to add or edit any item to the list. Same thing can be apply to SharePoint library also.
In this article I will create a event handler which will stop deletion from a SharePoint library.
So now follow below steps:
Create a class library in Visual studio and give a name to your class library such as "Event handler test".
Now you have to add reference of Microsoft SharePoint and Windows SharePoint Services. For this right click on project name in solution explorer and select Add reference.
Add 'using Microsoft.SharePoint;' this namespace in your code.

Now your code will look like below:


namespace EventhandlerDeletion
{
public class deleteevent:SPItemEventReceiver
{
public override void ItemDeleting(SPItemEventProperties properties)
{

properties.Cancel = true;

properties.ErrorMessage = "Deletion from this library is denied !!!";

}

}
}


Select your project which is last option in Project menu in Visual studio.
A new window will open, select Signing, select the check box 'sign the assembly' and from dropdown choose new and give any name.
Now press F6 to build your solution.
Go to this location,
C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\Your project name\your project name\bin\Debug

You will find a .dll of your application.
Drag this .dll file to C:\windows\assembly

Now go to this path:
C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES
Create new folder with name 'DeleteEvent'
In this folder you have to create two .xml files.
create first xml file i.e. feature.xml

Code for feature.xml:

<Feature Scope="Web"

Title="Event Handler To avoid Delete"
Id="1C579D91-4D4B-4212-B23F-9B10FD2FE304" xmlns="http://schemas.microsoft.com/sharepoint/">

<ElementManifests>

<ElementManifest Location="element.xml"/>

</ElementManifests>

</Feature>



Second xml file i.e. element.xml.
Code for element.xml:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

<Receivers ListTemplateId="101">

<Receiver>

<Name>Delete Event Handler</Name>

<Type>ItemDeleting</Type>

<SequenceNumber>22500</SequenceNumber>

<Assembly>DeletingEventHandler1, Version=1.0.0.0, Culture=neutral,

PublicKeyToken=4646756f239ca28f</Assembly>

<Class>EventhandlerDeletion.deleteevent</Class>

<Data></Data>

<Filter></Filter>

</Receiver>

</Receivers>

</Elements>



So till here you are done with all coding and configuration settings.
You have to install your feature now.
So open command prompt and prompt to bin location of hive folder (12 or 14).
and type this command:
stsadm -o installfeature -filename DeleteEvent(this is your folder name)\feature.xml

You need to activate this feature, you can do it by two ways:
One is in the command prompt type below command:
stsadm -o activatefeature -filename DeleteEvent(this is your folder name)\feature.xml -url your site url

Or you can see your feature in SharePoint site setting and then activate it from there.

Finally reset IIS by typing iisreset in command prompt.

Now you are done with event handler.
So create any document library and then try to delete an document from that library. You will see the particular message that you have mentioned in your code.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: