List Item Event Handler Using SharePoint Object Model

Introduction:

In this article I am going to explain you that How to create List Item Event handler using sharepoint object model.
You can bind a SharePoint Foundation event handler by using event receiver base classes in the SharePoint Foundation object model. Our goal in this Article is to bind the ItemAdded event .

Detail:-

Open Microsoft Visual Studio and create a basic console application project and name it BindItemEvents. Then create a single class named Program.

The code example above indicates the most commonly used property values. To see a full list of available properties; refer msdn link msdn. However, most important of the properties, and one that is required in all cases, is the Type property – in this case, ItemAdded. You must also specify the assembly by using the Assembly property, and the class – in this case, MyDefinition.ItemEvents.

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 System.Collections.Generic;
using System.Linq;
using System.Text;
 
using Microsoft.SharePoint;
 
namespace BindItemEvents
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Shared Documents"];
 
                    SPEventReceiverDefinition def = list.EventReceivers.Add();
 
                    def.Assembly = "MyDefinition, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=604f58e28557db00";
                    def.Class = "MyDefinition.ItemEvents";
                    def.Name = "ItemAdded Event";
                    def.Type = SPEventReceiverType.ItemAdded;
                    def.SequenceNumber = 1000;
                    def.Synchronization = SPEventReceiverSynchronization.Synchronous;
                    def.Update();
                }
            }
        }
    }
}

Below is the receiver class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using Microsoft.SharePoint;
namespace MyDefinition
{
    public class ItemEvents : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            SPListItem item = properties.ListItem;
            item["Title"] = item["Title"] + " - " + DateTime.Now;
            item.Update();
        }
    }
}


References:-

http://msdn.microsoft.com/en-us/library/ff407249.aspx

You may also like below article:
List Item Event Handlers Using SharePoint Features.xml
Create event handler for specific list

Thanks!
Avinash

calendarMarch 21, 2012 · cardInfoyen · commentsNo Comments
tagTags: , , , ,  · Posted in: Event Handler, List, MOSS, SharePoint

Leave a Reply

Spam Protection: , required

myworldmaps infoyen