Creating a SharePoint Event Handler

Applies to: SharePoint Foundation 2010

To create a SharePoint Foundation event handler in Visual Studio, create a new project of type "class library" and then inherit from one of the event receiver base classes. When you select the event receiver base class from which to inherit, be sure that your event receiver base is appropriate for the event you intend to capture and for the event host to which you intend to bind.

In the code sample below, we create an event receiver definition called ItemAdded that uses and overrides methods on the SPItemEventReceiver base class. We use this base class because we want to capture events that are raised when items have been added to a list. If we wanted to capture events on a Web site level instead, we would inherit from the SPWebEventReceiver class.

Important

To see a table that lists SharePoint Foundation events, the event receivers that are appropriate for each event, and the event hosts that can bind to each of the receivers, see Table of SharePoint Events, Event Receivers, and Event Hosts.

Creating an Event Receiver Definition

After creating your project, be sure to add a using Microsoft.SharePoint statement in addition to the project default using statements. In our project we create the ERDefinition namespace and declare a public class, ItemEvents, that we inherit from SPItemEventReceiver.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
namespace ERDefinition
{
    public class ItemEvents : SPItemEventReceiver
    ...

Our sample event receiver should detect when items are added to a list or document library, so we override the ItemAdded(SPItemEventProperties) method of the base class:

      public override void ItemAdded(SPItemEventProperties properties)

This method takes an SPItemEventProperties instance that provides information about the item properties.

The event handler in our example does very little, as you can see. We simply append a date-time stamp to the list item "Title" field, then call Update().

        {
            SPListItem item = properties.ListItem;
            item["Title"] = item["Title"] + " - " + DateTime.Now;
            item.Update();
        }

Now that we have created the event receiver definition, we must bind the event handler to the event host. See the continuation of this process in Binding a SharePoint Foundation Event Handler.

Code Listing

Following is the full code listing for the illustration above.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
namespace ERDefinition
{
    public class ItemEvents : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            SPListItem item = properties.ListItem;
            item["Title"] = item["Title"] + " - " + DateTime.Now;
            item.Update();
        }
    }
}

See Also

Tasks

How to: Create an Event Handler Feature

Concepts

Introduction to Events in SharePoint Foundation

Table of SharePoint Events, Event Receivers, and Event Hosts

Binding a SharePoint Foundation Event Handler