List Item Event Handlers Using SharePoint Features.xml

Introduction:

In this article I am going to explain you that How to create List Item Event handler using sharepoint feature.
There are various event types. For more detail on various event types refer MSDN link.
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 delete.

Detail:-

This article example shows the basic steps used to create an event handler. In this example, the 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.

You create an event handler assembly in Microsoft Visual Studio by creating a class library. Add a reference to Microsoft.SharePoint.dll and inherit from the Microsoft.SharePoint.SPItemEventReceiver base class.

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:-

listitem eventhandler


The Code
:-

We will create below 2 xml file and 1 feature receiver 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. Using the ElementManifests element, it then points to the location of the second XML file that stores all of the detailed information about the feature itself

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<Feature Scope="Web"
  Title="Simple Event Handler Registration"
  Id="A6B8687A-3200-4b01-AD76-09E8D163FB9A"
  xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>
  </ElementManifests>
</Feature>

Elements.xml

You use this file to define the assembly that encapsulates the event handler, the class itself, and also a sequence number that specifies the order, if multiple event handlers are associated with the Feature. The following example shows how to bind an event receiver that responds to the events for deleting and adding list items.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="104">
    <Receiver>
      <Name>MyEventHandlers</Name>
      <Type>ItemDeleting</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>MyEventHandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4358f2a5344ff0dc</Assembly>
      <Class>MyEventHandlers.SimpleEventHandler</Class>
      <Data></Data>
      <Filter></Filter>
    </Receiver>
    <Receiver>
      <Name>MyEventHandlers</Name>
      <Type>ItemAdded</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>MyEventHandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4358f2a5344ff0dc</Assembly>
      <Class>MyEventHandlers.SimpleEventHandler</Class>
      <Data></Data>
      <Filter></Filter>
    </Receiver>
  </Receivers>
</Elements>


Feature Receiver Class:-

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
38
39
40
41
42
43
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
 
namespace MyEventHandlers
{
    class SimpleEventHandler : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            if (properties.ListTitle == "MyList")
            {
                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)
        {
            if (properties.ListTitle == "MyList")
            {
                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
Create event handler for specific list

Thanks!
Avinash

calendarMarch 21, 2012 · cardInfoyen · commentsOne Comment
tagTags: , , , , , ,  · Posted in: Event Handler, List, MOSS, SharePoint

One Response

  1. Amritansh - May 26, 2012

    Awesome article. Simple and explained in a linear way…..
    Easy to understand

Leave a Reply

Spam Protection: , required

myworldmaps infoyen