EventHandling 
Google
Custom Search

Basic Event Handling in MOSS 

 

Also see other articles at: Sharepoint Blog


Let me start with what is event handling.

Definition of event handling “an event handler is an asynchronous callback subroutine that handles inputs received in a program”

Confused?? Ok in simple language event handling is doing some stuff when some predefined stuff happens. Example theft happens àpolice starts investigation.

Now here eventàtheft happens

Event handleràPolice(keeps a watch)

Event handler functionàInvestigation starts

Event handling is software is also the  same . In MOSS various events happen like adding a new item, deleting an item, creating a site,deleting a site etc

Now if we want to do some stuff whenever these events happen we need an event handler. Like if there is a theft an investigation can start only if there is a police officer

Now what are the types of crimes errr types of events an event handler can handle? Some event receiver base classes are listed below

 

Base Receiver

Events

SPWebEventReceiver

SiteDeleted

SiteDeleting

 

WebDeleted

 

WebDeleting

 

WebMoved

 

WebMoving

 

Base Receiver

Events

SPListEventReceiver

FieldAdded

FieldAdding

 

FieldDeleted

 

FieldDeleting

 

FieldUpdated

 

FieldUpdating

 

SPItemEventReceiver

ItemAdded

ItemAdding

 

ItemAttachmentAdded

 

ItemAttachmentAdding

 

ItemAttachmentDeleted

 

ItemAttachmentDeleting

 

ItemCheckedIn

 

ItemCheckedOut

 

ItemCheckingIn

 

ItemCheckingOut

 

ItemDeleted

 

ItemDeleting

 

ItemFileConverted

 

ItemFileMoved

 

ItemFileMoving

 

ItemUncheckedOut

 

ItemUncheckingOut

 

ItemUpdated

 

ItemUpdating

 

You need to write your custom code using these base classes for creating an event handler.

Now lets start and create our first event handler. Note this is not a hello world event handlers so you won’t see any hello world popping on your screen .The event handler that we will create will simply copy a newly added list item’s title into another list as simple as that.

1.    Go t visual studio and create a new class library project

2.    Give it any name you wish to(No restriction of MY_First_Event_handler J)

3.    Now delete the default class

4.    Add a reference to Microsoft.SharePoint.dll(May be visible with the name windows sharepoint services)

5.    Create a new class and inherit it from SPItemEventReceiver class

6.    Now since we need to trigger it on item added override the ItemAdded method(its like we tell the policeman what action to take when a murder happens)

7.    Argument for this function is SPItemEventProperties properties . this variable properties can be used to access the properties of an event like itemname, itemId,Web name,site name etc(its like initial FIR who was murdered,where did the murder took place etc etc)

8.    Using this write your custom code. See the code snippet below

 

 

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

namespace __madhur_ClassLibrary1_event

{

class ApplicationEventReceiver : SPItemEventReceiver

{

public override void ItemAdded(SPItemEventProperties properties)

{

SPWeb web = properties.OpenWeb();

SPList list = web.Lists["Events"];

SPListItem item = list.Items.Add();

list.Update();

item["Title"] = properties.ListItem.Title;

item.Update();

web.Close();

}

}

}

 

9.    Next step is to sign your assembly ,compile it and put I into GAC(global assembly cache)

10. Now we have told the policeman what he needs to do when a murder happens. But should he start his investigation whenever a murder happens, anywhere? I mean should a policeman in Punjab start investigating when someone is murdered in Honolulu .No, we need to assign him an area. Like you will investigate only if something happens in suppose sector 6,7,8 Chandigarh .Similarly we need to add our event handler to some list/library.So that it fires only when an item is added to that list

11. To do this we need to write a console application.See the code sample below

 

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

namespace EventReceiverRegister

{

class Program

{

static void Main(string[] args)

{

SPSite site = new SPSite("<site collection address>");

SPWeb web = site.OpenWeb("");

//list to which the event receibver is attached

SPList list = web.Lists["EventGenearte"];

//assembly details of event handler

//the public key token of the dll can be taken from GAC

string assemblyname = " __madhur_ClassLibrary1_event,Version=1.0.0.0,culture=neutral,PublicKeyToken=b82dc21758d228b3";

string classname = " __madhur_ClassLibrary1_event.ApplicationEventReceiver";

list.EventReceivers.Add(SPEventReceiverType.ItemAdded, assemblyname, classname);

Console.WriteLine("Registered");

Console.Read();

}

}

}

 

That’s it!!

NOOO.Please run this console application once

Now you can say that’s it. Now your event hadler is ready.. Whenever any new item is created in the list named EventGenearte a new item with the same title is created in the list called Events