Writing a simple Event Reciever or Event Handler in Sharepoint


What is Event handling ?

The ability to catch certain user actions in code and respond programmmatically. These are the classes which we can inherit and write event handlers or receivers against them.

SPItemEventReceiver

SPListEventReceiver

SPWebEventReceiver

Which user actions you can catch?

ItemFileMove,IFMoving, ItemUpdated,ItemUpdating, ItemCheckedIn/Out, ItemCheckingIn/ItemChecingOut, ItemAdded,IAdding, ItemAttachementAdded/Adding, ItemDeleted/Deleting.

What is template id for customlist, document library, survey?

100 Custom List 101 Document Library 102 Survey 103 Links List 104 Announcements 105 Contacts

106 Events List 107 Tasks List 108 Discussion Board 109 Picture Library

Explain the steps for creating event handlers?

1) Create a class library project.

2) Add reference to Microsoft.Sharepoint.dll

3) Inherit the class SPItemEventReceiver

4) Override the method against which you want to attach the event handler.

e.g. for itemdeleting

namespace MyEventHandler{

public class ItemDeletingHandler : SPItemEventReceiver{

public override void ItemDeleting(SPItemEventProperties properties){

// We want to attach the eventhandler against a document library

// named MyDocumentLibrary

if(properties.ListTitle == “MyDocumentLibrary”){

// only the admin user has right to delete the uploaded document

if (!properties.UserLoginName.Contains(“domainnameadminUserName”)){

properties.ErrorMessage = “You don’t have sufficient privelege to delete on list “+properties.ListTitle;

properties.Cancel = true;}}}}}

5) Sign the assembly and put it in GAC i.e. c:windowsassembly

6) Create a new folder with the same name as your eventhandler at the following path

c:program filescommon filesmicrosoft sharedweb server extension12templateFeatures folder

7) Create two files feature.xml and elements.xml at the newly created folder.

8) Take feature.xml file of announcement and make the necessary changes to it.

<?xml version=1.0 encoding=utf-8?>

<Feature Id=C50FF499-B52A-471f-A9FB-36A49E2FA49F

Title=MyEventHandler

Description=For restricting delete of a list

Scope=Web

xmlns=http://schemas.microsoft.com/sharepoint/>

<ElementManifests>

<ElementManifest Location=Elements.xml/>

</ElementManifests>

</Feature>

9) Within elements.xml specify

<?xml version=1.0 encoding=utf-8?>

<Elements xmlns=http://schemas.microsoft.com/sharepoint/>

<Receivers ListTemplateId=101>

<Receiver>

<Name>MyEventHandler</Name>

<Type>ItemDeleting</Type>

<SequenceNumber>20000</SequenceNumber>

<Assembly>

MyEventHandler, Version=1.0.0.0,

culture=neutral, PublicKeyToken=e185cb2eba5d0774

</Assembly>

<Class>MyEventHandler.ItemDeletingHandler</Class>

<Data></Data>

<Filter></Filter>

</Receiver>

</Receivers>

</Elements>

Get the values for assembly tag by right clicking the assembly and selecting properties within the GAC.

If we have multiple eventhandler attached to a same event we can specify which handler should be executed first through SequenceNumber tag. It’s value should start from 20000

10) Now install the feature

stsadm.exe -o installfeature -filename MyEventHandlerfeature\Feature.xml

11) Activate the feature

stsadm -o activatefeature -filename MyEventHandlerfeature\Feature.xml -URL http://localhost.

That’s it…


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

9 thoughts on “Writing a simple Event Reciever or Event Handler in Sharepoint”

  1. Hi Nishant,

    In my list in Sharepoint, I have field called status which is updated to Complete whenever an order is complete. I am looking for way to notify the user who is associated with that order. The user’s email id is stored in a field called Email in the same list and everytime the status of an order is updated to complete, I want to send the user a survey link in the email. I want to pass the order number for that order as an url parameter when the event happens(when status is complete). I am working with sharepoint 2003 and do not see the event class that you have mentioned in your article. Can you please share your thoughts on this?

    Like

    1. Hi, Even i haven’t worked on SharePoint 2003, so don’t know much about it !!

      This is what I found while looking for the same !!

      “Events in WSS 2.0 supported document libraries but not lists. Moreover, version 2 supported only asynchronous events that fired after a user action had been committed to the SQL Server database. There was no way for the developer to cancel a user’s action inside an event handler”

      Seems quite restrictive !

      Like

  2. I’ve tried to do this to stop users updating items but doesn’t work here is my code

    namespace StopEditOverRide
    {
    public class StopEditOverRide : SPItemEventReceiver
    {

    public override void ItemUpdating(SPItemEventProperties properties){

    if(properties.ListTitle == “Document Approval”){

    if (!properties.UserLoginName.Contains(“euaLDN-C5849”)){

    properties.ErrorMessage = “You don’t have sufficient privelege to edit on list”+properties.ListTitle;

    properties.Cancel = true;
    }}}}}

    Can you help

    Like

  3. 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.

    Like

  4. Magnificent goods from you, man. I have understand your stuff previous to and you are just extremely
    wonderful. I really like what you’ve acquired here, certainly like what you’re saying
    and the way in which you say it. You make it entertaining and you still care for to keep it
    sensible. I can’t wait to read much more from you. This is really a great web site.

    Like

  5. Right here is the perfect web site for anyone who would like to understand this topic.
    You know a whole lot its almost hard to argue with you (not that I really would
    want to…HaHa). You certainly put a new spin on a subject that has been discussed for
    decades. Great stuff, just wonderful!

    Like

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.