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

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

Code project profile CodeProject

10 comments:

  1. Nice Article.. Thank you !

    ReplyDelete
  2. Very nice example and explained in very well manner
    Thanks a lot

    ReplyDelete
  3. How do I define or find what Web site is associated with the ItemAdded event. I'm trying understand where the value that is returned from the properties.OpenWeb() call is defined. Thanks for any help you can provide.

    ReplyDelete
  4. http://code.google.com/p/sharepoint-eventhandlers-manager/

    SharePoint Event Handlers Manager allows admins to browse, add, edit and remove SharePoint event handlers from any list or web. This SharePoint solution provides two features that enables admins to play with event handlers from within SharePoint interface.

    It is very simple to use and doesn't require any configuration to start. Just download and install, and you are all set.

    ReplyDelete
  5. Whats up very nice blog!! Guy .. Beautiful ..
    Amazing .. I'll bookmark your web site and take the feeds also? I am happy to seek out a lot of useful info right here within the post, we want work out more strategies on this regard, thank you for sharing. . . . . .

    Feel free to surf to my page :: http://xxx-fuck.net/

    ReplyDelete
  6. My developer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the costs.
    But he's tryiong none the less. I've been
    using Movable-type on a variety of websites for about
    a year and am worried about switching to another platform.
    I have heard great things about blogengine.net. Is there a way I can import all my wordpress content into
    it? Any help would be greatly appreciated!

    my web-site de estrellas foto porn ()

    ReplyDelete
  7. I am really enjoying the theme/design of your blog. Do
    you ever run into any internet browser compatibility problems?
    A couple of my blog audience have complained about my website not working correctly in
    Explorer but looks great in Chrome. Do you have any solutions to help fix this issue?


    My website: Juicy Big Tits Teen

    ReplyDelete
  8. Grate article, We at Addhunters shifted this service to a level much higher than the broker concept.
    you can see more details like this article
    For sale in lusail

    ReplyDelete