Translate

Sunday, August 19, 2012

Creating Event Handlers in SharePoint 2010

Using Visual Studio 2010 Creating sharepoint Event Handlers

Sharepoint Event Handlers


There are two types of event receivers are available in sharepoint<br />


1.Synchronous
Synchronous event receiver is also know as before event receivenrs
These events are fired before doing any operations on list
For example
ItemAdding is one event 
ItemDeleting is one more event which is performed on a List Item

2.Asynchronous
Asynchronous events are those which happen after any operation is being performed on list item
For example
ItemAdded,ItemDeleted
actually these are events used for business flow purpose
for example if you want to send an email to the user whenever new item is added to the list


We can implement event receiver for the following
1.List Events
2.List Item Events
3.List Email Events
4.Web Events
5.List Workflow Events
6.Feature Events

 
Steps to create List Item Events receivers

Step 1.Open Visual studio 2010

Step 2.Select the event receiver template as shown in the following figure


Step 3.Select the select which you want to bind the event receiver

Step 4.Visual studio will ask to select which event you need to add as shown in the following figure


Step 5.Click on Finish



I am using a sample of event receiver where i am not allowing the user to delete any item from the list


using Microsoft.SharePoint.Saecurity;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace EventHandlersSample

{
    public class SimpleDeleteEventHandler : SPItemEventReceiver    {

public override void ItemDeleting(SPItemEventProperties properties)
{
properties.Cancel = true;
    properties.ErrorMessage = "Deleting is not authorized.";

   
}
    }
}

Step 6.Once code is done just press F5 the event receiver will be attached the selected list and you debug

In visual studio 2010 by default it creates a feature for the event handler but if you are using visual studio 2008 we need to create the feature manually

In SharePoint 2010 there is a provision to add our own custom message page instead of using SharePoint default error page.


No comments:

Post a Comment