How to work with Event Handler step by step in Share Point?

How to work with Event Handler step by step in Share Point?

Even handler is executed when some operation occurs in SharePoint site.
Suppose you want to add event handler when an item will be added, or item will be deleted from a document library.
So, i am going to create an event handler that will avoid deletion from a document library in Share Point.
Open Visual Studio 2008.
From Project type select C# and from template select Class Library.
i.e. simply you have to create a Class Library in Visual Studio.

Give name to your Class Library application.

Right click on project in solution explorer.

Add Reference…

Then from .Net tab, add Microsoft SharePoint and Windows SharePoint Services

Now use this namespace.

using Microsoft.SharePoint;

So, your code will look like this.

namespace DeletingEventHandler1
    public class myevent1:SPItemEventReceiver
        public override void ItemDeleting(SPItemEventProperties properties)

            properties.Cancel = true;

            properties.ErrorMessage = "You can not delete items from here !!!";


Now Open Project menu in Visual Studio. Select your project that will be last option in the menu.
From the new window select Signing, tick on the check box “sign the assembly” and from drop down list select new and give name. “No need to give any password”.
Now, Build your solution.
Open this location
C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\Your project name\your project name\bin\Debug.
Here you will find a .dll file with same name as your application.
Now drag this .dll file in to C:\Windows\assembly
(Note:Copy-paste will not work, just drag & drop)
Again locate this location
Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES
Create a folder myevent.
In that folder create 2 xml files. i.e. feature.xml and element.xml
<Feature Scope="Web"
    Title="Event Handler To avoid Delete"
    Id="1F579D91-4F4B-4389-B03B-7A92EF2EE210" xmlns="">
<ElementManifest Location="elements.xml"/>  
(Note: id can be found from Visual studio, Tools->Create GUID, copy a id and paste it here)

<Elements xmlns="">
<Receivers ListTemplateId="101">
    <Name>Event Handler-Delete Avoid</Name>
    <Assembly>DeletingEventHandler1, Version=, Culture=neutral,
(Note:You may get public token from C:\Windows\assembly\your.dll file. Right click on your .dll file, select properties, now you will see the public token, copy that and paste.)
 Now Open Command Prompt and do the below steps.

stsadm -o installfeature -filename yourfoldername\feature.xml
stsadm -o activatefeature -filename yourfoldername\feature.xml -url your site url.
Now create a document from Document Library and try to delete it.

Popular posts from this blog

Calling REST APi from server side code - C#

All about SharePoint 2010 Content Type Hub

Calling ASP.Net WebMethod using jQuery AJAX