TechBubbles Microsoft Technology BLOG

Creating a custom event handler in SharePoint 2010

SharePoint fires different events when you create custom event receivers. The events triggered by SharePoint are List Item events, List Email events, List Workflow events and Feature events. These can further classified into synchronous and asynchronous events. Synchronous events are executed on the same thread as the code, before sending response to the browser. Asynchronous events are those that take place after the action has happened.

Event handlers in SharePoint used for validating an item before it is adding it to the list. Sending a custom e-mail when an item is added to a list. Logging to external database for audit purpose. You can package the event receivers as a solution file to deploy to the SharePoint event host.Visual Studio 2010 supports project template for creating event receivers.This post explains how to create a custom event handler for the ItemAdding event.

1. Create a list in SharePoint user interface with name Contacts of template contacts.

image 

2. Launch Visual Studio 2010 and create a new project and select EventReceiver project template from the dialogue box

image

3. Visual Studio by default selects the SharePoint site available on the machine. Select Deploy as a Farm Solution and click on Next to proceed to next step

4. Select List Item Events from the drop-down  that you want to add and select event source as shown below

image

5. Add the code to the event receiver to validate the items before it is getting added to the list

   1: using System;

   2: using System.Security.Permissions;

   3: using Microsoft.SharePoint;

   4: using Microsoft.SharePoint.Security;

   5: using Microsoft.SharePoint.Utilities;

   6: using Microsoft.SharePoint.Workflow;

   7: using System.Text.RegularExpressions;

   8: namespace ListItemEventReceiver.EventReceiver1

   9: {

  10:  /// <summary>

  11: /// List Item Events

  12: /// </summary>

  13: public class EventReceiver1 : SPItemEventReceiver

  14: {

  15: /// <summary>

  16: /// An item is being added.

  17: /// </summary>

  18: public override void ItemAdding(SPItemEventProperties

  19: properties)

  20: {

  21: base.ItemAdding(properties);

  22: string sWorkPhone = properties.AfterProperties

  23: ["WorkPhone"].ToString();

  24: if (!string.IsNullOrEmpty(sWorkPhone))

  25: {

  26:   //Validate logic goes here, cancel if it not pass

  27:   properties.Cancel = true;

  28:  

  29: }

  30:  

  31: }

  32: }

  33: }

6. Build and execute the solution that brings the default browser with contact list

7. If you look at the element.xml file then you will notice properties such as Name, Type and ListTemplateId

   1: <?xml version="1.0" encoding="utf-8"?>

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

   3: <Receivers ListTemplateId="105">

   4: <Receiver>

   5: <Name>EventReceiver1ItemAdding</Name>

   6: <Type>ItemAdding</Type>

   7: <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>

   8: <Class>ListItemEventReceiver.EventReceiver1.EventReceiver1</

   9: Class>

  10: <SequenceNumber>10000</SequenceNumber>

  11: </Receiver>

  12: </Receivers>

  13: </Elements>

SharePoint determines the order of execution of the event handlers by using Sequence Number element.

8. Visual Studio adds another project item called Features. If you have custom code that makes the changes to site templates and creates various lists then you can deploy this code across all web front ends in SharePoint farm.

image

9. When you build and run your Visual Studio solution it automatically creates a package and deployed to test site and activate it.

10. Event receivers you create in Visual Studio inherits from particular SharePoint event receiver class. All event receivers except Feature event receivers are inherited from base class called SPEventReceiverBase

Share this post :

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

Add Comment

TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud