What is Event handler in SharePoint ? How to use it in a SharePoint site?
This article will tell you about event handler used in sharepoint. Event handler perform a action depending up on your code. It helps you to perform an action by you. Learn about Event handler in SharePoint? How to use Event handler in a SharePoint site? Here I am providing code and few steps for event handler.
Learn What is Event handler in SharePoint? How to use it in a SharePoint site?
I will create an event handler which will avoid deletion from a document library in your SharePoint site.
Follow these steps for Event handler:
Open Visual studio, File-New-Project
Choose C# as your language and Class library from the template section.
So before writing the code you need to add few references. Right click on your solution and select Add Reference... From .net tab select Windows SharePoint Services and Microsoft SharePoint.
Add this namespace in your class library:
using Microsoft.SharePoint;
namespace EventhandlerCode
{
public class eventhandlersample:SPItemEventReceiver
{
public override void ItemDeleting(SPItemEventProperties properties)
{
properties.Cancel = true;
properties.ErrorMessage = "Sorry! You can't delete items from this library";
}
}
}
Now build your application. It should succeed, if yes then fine. (Do not run your application now)
Open project menu from Visual studio, select your project name from the menu.
From new window select Signing tab, select the checkbox "sign the assembly" and from dropdownlist select new and provide a name. Leave it as it is for other options. Build your solution again.
Locate this path: C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\Your project name\your project name\bin\Debug
Drag .dll file of your project name in to C:\Windows\assembly
Go to this location.
Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES
Create a folder EventHandler.
In this folder create 2 xml files.
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="Element.xml"/>
</ElementManifests>
</Feature>
Element.xml
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="101">
<Receiver>
<Name>No Deletion</Name>
<Type>ItemDeleting</Type>
<SequenceNumber>22500</SequenceNumber>
<Assembly>DeletingEventHandler1, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=4646756f239ef28b</Assembly>
<Class>EventhandlerCode.eventhandlersample</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
</Receivers>
</Elements>
Open command prompt and prompt to
C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/BIN
Type below commands one by one
Your task is completed now. Open your sharepoint site create a new document library, add few documents and try to delete any one of them. It will give you a warning message that you can delete.
Note: It will work for all document libraries of specified url.
Do not forget to give your valuable feedback.
SharePoint Event Handlers Manager allows admins to browse, add, edit and remove SharePoint event handlers from any list or web. This SharePoint solution provides two features that enables admins to play with event handlers from within SharePoint interface.
http://code.google.com/p/sharepoint-eventhandlers-manager/