How to: Create an Event Receiver
Event receivers are classes that allow you to respond to events that occur to SharePoint items such as lists or list items. When someone changes the calendar or deletes a name from the contacts list, the code in the event receiver is triggered.
This how-to demonstrates the following tasks:
- Adding an event receiver to a list instance.
You need the following components to complete these steps:
Supported editions of Microsoft Windows and SharePoint. For more information, see Requirements for Developing SharePoint Solutions.
Visual Studio 2010.
You also must complete the procedure in the topic Walkthrough: Create a Custom Field, Content Type, List Definition, and List Instance.
Adding an Event Receiver
The project that you created in Walkthrough: Create a Custom Field, Content Type, List Definition, and List Instance includes a custom field, a custom list definition with an instance of that list, and a content type. Next, you will add an event receiver to that project. The following procedure adds a simple event handler (an event receiver) to the list instance.
To add an event receiver to the list instance
Open the project that you created in Walkthrough: Create a Custom Field, Content Type, List Definition, and List Instance.
Add an event receiver to this project. To do this, click the project node in Solution Explorer and then select Add New Item on the Project menu.
Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.
In the Templates pane, select Event Receiver and name it TestEventReceiver1.
The SharePoint Customization Wizard appears.
On the Choose Event Receivers page, select List Item Events as the event receiver type.
Set the Event source item to CustomField1 - ListDefinition1.
In the list of events to handle, check the box next to An item was added and then click Finish.
The new event receiver code file contains a single method named ItemAdded. Add code to this method so that every time a contact is added to the contact list, a default Patient Name value of Scott Brown is used. To do this, replace the existing ItemAdded method with the following:
Public Overrides Sub ItemAdded(ByVal properties As SPItemEventProperties) properties.ListItem("Patient Name") = "Scott Brown" properties.ListItem.Update() MyBase.ItemAdded(properties) End Sub
public override void ItemAdded(SPItemEventProperties properties) { properties.ListItem["Patient Name"] = "Scott Brown"; properties.ListItem.Update(); base.ItemAdded(properties); }
Press F5 to run the code and view the SharePoint site in the Web browser.
On the QuickLaunch bar, click CustomField1 - ListInstance1.
This is the List Instance for the list definition. Note that the Patient Name field does not yet appear as a column in the list instance.
Click List in the List Tools tab at the top of the page and then click the Modify This View button on the ribbon.
In the list of available column names, select Patient Name and then click OK. Notice that the Patient Name field now appears in the list instance.
On the List Tools tab at the top of the page, click Items and then the New Item button to display the data entry form.
Enter data in the fields and then click the Save button in the list instance's new item dialog box.
The Patient Name column automatically populates with Scott Brown after you click OK.