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.
File->New->Project
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 SettingsAdministratorMy DocumentsVisual Studio 2008ProjectsYour project nameyour project namebinDebug.
Here you will find a .dll file with same name as your application.
Now drag this .dll file in to C:Windowsassembly
(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.xml
<Feature Scope=”Web”
    Title=”Event Handler To avoid Delete”
    Id=”1F579D91-4F4B-4389-B03B-7A92EF2EE210″ xmlns=”http://schemas.microsoft.com/sharepoint/”>
<ElementManifests>  
<ElementManifest Location=”elements.xml”/>  
</ElementManifests>
</Feature>
(Note: id can be found from Visual studio, Tools->Create GUID, copy a id and paste it here)

element.xml
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Receivers ListTemplateId=”101″>
    <Receiver>
    <Name>Event Handler-Delete Avoid</Name>
    <Type>ItemDeleting</Type>
    <SequenceNumber>22500</SequenceNumber>
    <Assembly>DeletingEventHandler1, Version=1.0.0.0, Culture=neutral,
    PublicKeyToken=4646756f239ef28b</Assembly>
    <Class<DeletingEventHandler1.myevent1</Class>      
    <Data></Data>
    <Filter></Filter>
    </Receiver>
</Receivers>
</Elements>
(Note:You may get public token from C:Windowsassemblyyour.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 yourfoldernamefeature.xml
stsadm -o activatefeature -filename yourfoldernamefeature.xml -url your site url.
iisreset
Now create a document from Document Library and try to delete it.

Leave a Comment

RSS
YouTube
YouTube
Instagram