SharePoint 2010 – Lists and Event Handlers

In this article we can explore the Event Handling features of List through code. Using the SPList in Server Object Model we can access event handling for a list.

We can use the event to perform the following activities:

· Validate the Item

· Log the information

· Create associated items

There are multiple Event Types for a List:

· List Events

· List Item Events (Add, Edit, Delete)

· Web Events

· Feature Events

· Workflow Events

Create Event Receiver Project

For starting with create an Event Receiver project inside Visual Studio.

clip_image002

You will be prompted for the site:

clip_image004

By default the machine site will be shown in the dialog box. Leave the default option of Sandboxed solution and click the Next button.

clip_image006

Select the List Item Events and use event source as Contacts. Check the check boxes for added, updated and deleted events and click the Finish button.

Note: The site url can be changed later. For time being we are using hard coded urls.

On clicking Finish button, the code will be generated for the List Event Receiver and you can place a break point in the ItemAdding event as shown below.

clip_image008

Now execute the application and your Event will be added and activated.

Try adding a new contact inside SharePoint:

clip_image010

On clicking the Save button, the breakpoint inside Visual Studio will get hit:.

clip_image012

SPItemEventProperties

You can cancel the operation by using the Cancel property.

You can report an error message using the ErrorMessage property.

Cancel for Delete

You can cancel and item delete by setting the properties.Cancel = true;

public override void ItemDeleting(SPItemEventProperties properties)

{

properties.ErrorMessage = "Deleting Item is not permitted!";

properties.Cancel = true;

}

Try deleting an item from the Contacts inside SharePoint:

clip_image014

You will get the following message prompted:

clip_image016

Deploying the Event Receiver

You can take a note that while you stop Visual Studio, the item deletion is allowed inside SharePoint. To make the event receiver permanent, use the project Deploy option.

clip_image018

Now try Deleting an item without Visual Studio Debugging. You will get the same Error Message Dialog and this concludes the Event Creation and Deployment to a SharePoint site.

Properties of Project

You can change the Url and other properties created through the Wizard. Use Project > Properties to access/modify these properties.

clip_image020

Features

The event receiver is actually deployed as a feature inside SharePoint. You can view the Feature properties and the associated xml file under Features special folder as shown below.

clip_image022

The Event code and Elements.xml reside inside the EvntReceiver1 group as shown below.

clip_image024

References

http://msdn.microsoft.com/en-us/library/bb736146(v=office.12).aspx

Summary

In this article we have explored the List Event facility of SharePoint. In advanced List Management these flexibilities are great tool for a SharePoint Developer.

2 thoughts on “SharePoint 2010 – Lists and Event Handlers

  1. kumar says:

    Hi jean,
    how to apply Event Receiver for a particular list,
    i.e i had created 5 custom lists ,but i need event receiver for only one custom list.

    regards
    kumar

    1. Jean Paul says:

      Hi Kumar,

      You can add a new Event Receiver inside Visual Studio. As part of the wizard it prompts for the List to set event for.

      The event xml file will say the ListTemplateId like this:

      As the above code is targetting List Template instead of List Instance, it will fire for all list of same templates.

      If you have to fire only for one list instance, use the ListUrl atttribute instead and set the event scope to Web.

      Good luck!

      Regards,
      Jean Paul

Leave a Reply

Your email address will not be published. Required fields are marked *