How to: Create an Event Handler Feature

Applies to: SharePoint Foundation 2010

This example shows how to add a simple event receiver using SharePoint development tools in Microsoft Visual Studio 2010. The event receiver prevents items from being deleted from a task list.

To create the event handler

  1. Start Microsoft Visual Studio 2010.

  2. On the File menu, point to New, and then click Project.

  3. In Project Types, under Visual Basic or C#, select Event Receiver.

  4. Type DeletingEventReceiver as the project name. Click OK.

  5. In the SharePoint Customization Wizard, choose Deploy as a sandboxed solution. Click Next.

  6. In the Choose Event Receiver Settings dialog, select List Item Events in the What type of event receiver do you want? dropdown.

  7. In the What item should be the event source? dropdown, choose Tasks.

  8. Choose the An item is being deleted option in the Handle the following events list. Click Finish.

  9. In the EventReceiver1 file that is created, insert the following code in the ItemDeleting method.

    properties.Status = SPEventReceiverStatus.CancelWithError;
    properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";
    properties.Status = SPEventReceiverStatus.CancelWithError
    properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported."
  10. Press F5 to deploy the solution.

  11. Navigate to a task list and select an item in the list. Click the Delete Item button on the Server ribbon.

  12. Observe the error message.

See Also



Event Registrations

Other Resources

Events in SharePoint Foundation 2010