Add Event Receivers to SharePoint 2010 Lists

SharePoint QuickStart Banner

Getting Started with Web Development in SharePoint 2010:  Learn how to attach event receivers to custom SharePoint 2010 lists.

Applies to: Office 2010 | SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2010

Published:  June 2010

Provided by:   Frank Rice, Microsoft Corporation

In this exercise, you add an event receiver to a custom Microsoft SharePoint 2010 list that is triggered when you attempt to delete an item in the list. To complete this task, you must do the following:

  • Open the List Definition Project

  • Add the Event Receiver Code

  • Test the Solution

Note

This exercise assumes that you already created the custom list project titled Bugs in Microsoft Visual Studio 2010. Instructions for creating the custom list definition project are contained in the Quick Note Create Custom List Definitions in SharePoint 2010.

Open the List Definition Project

In this task, you open the list definition project in Visual Studio 2010.

To open the list definition project

  1. Start Visual Studio 2010

  2. On the File menu, point to Open, and then click Project/Solution.

  3. Navigate to the list definition project file (Bugs.sln) and then click Open.

Add the Event Receiver Code

In this task, you create and attach an event receiver to the list definition. This event receiver prevents the deletion of bug items and displays a message.

To add the event receiver to the list definition

  1. In Solution Explorer, right-click the Bugs node, point to Add, and then click New Item.

  2. In the New Project dialog window, in the Installed Templates section, click Visual C#, click SharePoint, and then click 2010.

  3. From the project items, click Event Receiver.

  4. Type BugListItemEvent in the Name box, and then click Add.

  5. In the SharePoint Customization Wizard, select the An item is being deleted option, and then click Finish.

  6. In Solution Explorer, expand BugListItemEvent, and then open the Elements.xml file.

  7. To make sure that the event receiver only binds to the custom list, ensure the ListTemplateId attribute in the Receivers element is set to 10001.

  8. In Solution Explorer, expand BugListItemEvent and then open BugListItemEvent.cs. You should see the overridden ItemDeleting method.

  9. Insert the following code into the body of ItemDeleting method after the base.ItemDeleting(properties); statement. This code prevents the deletion of an item and display an error message.

    try
    { 
       properties.Cancel = true;
       properties.ErrorMessage = "Bugs can only be resolved not deleted!";
    }
    catch (Exception ex)
    {
       return;
    }
    finally
    {
       this.EventFiringEnabled = true;
    }
    
  10. In Solution Explorer, right-click the Bugs node and then click Deploy. If you get a deployment error message, click Resolve Automatically.

  11. Open the website you specified previously.

  12. On the Home page, in the left navigation pane, click the Bugs list.

  13. On the List Tools tab, click Items, and then on the New Item drop-down list, click Bug Item. The Bugs - New Item screen is displayed.

  14. To create a bug, type information into the applicable boxes, and then click Save.

  15. Next, try to delete the item. You should receive a message similar to the one shown in Figure 1.

    Figure 1. Error message triggered by trying to delete the item

    Error message when trying to delete

Test the Solution

In this task, you first create an item in the custom list and then trigger the event receiver by trying to delete the item.

To test the solution

  1. Open the website you specified in the list definition project.

  2. On the Home page, in the left navigation pane, click the Bugs list.

  3. On the List Tools tab, click Items, and then on the New Item drop-down list, click Bug Item. The Bugs - New Item screen is displayed.

  4. To create a bug, type information into the applicable boxes, and then click Save.

  5. Next, try to delete the item. You should receive a message similar to the one shown in Figure 1.

Next Steps