Wednesday, July 29, 2009

Basic Event Handling in Sharepoint

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







Base Receiver


























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();


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






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("");

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=,culture=neutral,PublicKeyToken=b82dc21758d228b3";

string classname = " __madhur_ClassLibrary1_event.ApplicationEventReceiver";

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






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

Code project profile CodeProject

Friday, July 17, 2009

Developer resources on sharepoint

a good collection of places where you could find developement resources for sharepoint

checking if a sharepoint list exists or not

One of the major problem is sharepoint object model is handeling lists....
what to do is a list does not exist??
Once we needed to write a code that would rename a list if it to do that we neede to put the
web.lists["listname"] in try catch block....doing it again and again is really a pain...
i read this cool way of writing extension methods.on Bill's (a MVP) blog...
wat it will eseesntially do is ,it will create an extension method through which you could check something like
have a look...simple but a pretty good idea..
thanks bill

Amazing link for beginning sharepoint

i just love this UI...its intutive,its siple and to top it all its gorgeous....also the theme is Black my fav :D

WSS is not free

One day during a discussion on WSS vs MOSS  a point that came up was
WSS is free...creating an application on WSS would involve no liscencening cost,but is it so??
a quick initial search gave me the answers i was looking for
Though WSS in itself might be free but we do forget thet we still need CALs for SQL server.Though you may argue that its essentially WSS that interacts with SQL and not the users directly ,you still need to have a CAL foe each and every user accesing the WSS application that you develop.
This shoud not be a problem if you have enterprise wide liscence for SQL...or else you do need it
So saying that WSS is entirely free is not soo true,yes if you compare MOSS and WSS its essentially free because you save on MOSS Liscensining  cost...but still SQL is there....