Create event handler for specific list
Introduction:
In this article I will describe that how to create event handler for specific list using sharepoint feature.
Here I will take example of Item Added & deleting action event. On added event, we will add item description and on deleting event, we will not allow to delete.
Detail:-
This article example shows the basic steps used to create an event handler. In this example, feature receiver class add event handler on list and then event handler executes code before a list item is deleted or after an item is added. The example works on announcement lists, adding text to the body of new items and cancelling attempts to delete existing items.
The example overrides two methods: ItemDeleting and ItemAdded. Remember that the suffix “-ing” indicates that the event is being handled before the action occurs, and the suffix “-ed” indicates that the event is being handled after the action occurs.
Please see below solution structure and Code.
Solution Structure:-
The Code:-
We will create below feature.xml file and 1 featurereceiver.cs file. Then rest is usual WSP deployment on sharepoint.
Feature.xml
You use this XML file to define the metadata for the new feature. The following example code scopes the feature at the level of the site and defines a unique identifier for the new feature.
1 2 3 4 5 6 | <Feature Id="write feature id here" Title="HomeProjectRND" Description="This feature demonstrates feature receiver implementation." Version="1.0.0.0" Scope="Web" ReceiverAssembly="Home.Project.Solution, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f2w533b16a1234h" ReceiverClass="Home.Project.Solution.AvinashFeatureRNDReceiver" Hidden="FALSE" xmlns="http://schemas.microsoft.com/sharepoint/"> </Feature> |
Feature Receiver Class:-
This class will add event handler on sharepoint list named as “MyList”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | using System; using Microsoft.SharePoint; namespace Home.Project.Solution { public class AvinashFeatureRNDReceiver : SPFeatureReceiver { public override void FeatureInstalled(SPFeatureReceiverProperties properties){} public override void FeatureUninstalling(SPFeatureReceiverProperties properties){} public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPWeb web = (SPWeb)properties.Feature.Parent; SPList list = web.Lists["MyList"]; list.EventReceivers.Add(SPEventReceiverType.ItemDeleting, "Home.Project.Solution, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4358f2a5344ff0dc", "Home.Project.Solution.SimpleEventHandler"); list.EventReceivers.Add(SPEventReceiverType.ItemAdded, "Home.Project.Solution, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4358f2a5344ff0dc", "Home.Project.Solution.SimpleEventHandler"); // Similarly you can add other events also if required. list.Update(); } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { } } } |
Event Receiver Class:-
This class have Item Added & deleting action event. On added event, it will add item description and on deleting event, it will not allow to delete.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; namespace Home.Project.Solution { class SimpleEventHandler : SPItemEventReceiver { public override void ItemAdded(SPItemEventProperties properties) { try { SPListItem oItem = properties.ListItem; oItem["Body"] = "Body text maintained by the system."; oItem.Update(); } catch (Exception ex) { //handle exception } } public override void ItemDeleting(SPItemEventProperties properties) { try { properties.Cancel = true; properties.ErrorMessage = "Deleting is not supported."; } catch (Exception ex) { //handle exception } } } } |
Reference:-
msdn link: Event Handlers by Using SharePoint Features.xml
msdn link: Creating a List Item Event Handler
You may also like below article:
List Item Event Handler Using SharePoint Object Model
List Item Event Handlers Using SharePoint Features.xml
Thanks!
Avinash
April 29, 2012
В·
Infoyen В·
No Comments
Tags: Create an Event Handler, Deploying Item Event Receiver, MOSS, SharePoint В· Posted in: Event Handler, MOSS, SharePoint
Leave a Reply