Export (0) Print
Expand All

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:

You also must complete the procedure in the topic Walkthrough: Create a Custom Field, Content Type, List Definition, and List Instance.

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

  1. Open the project that you created in Walkthrough: Create a Custom Field, Content Type, List Definition, and List Instance.

  2. 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.

  3. Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.

  4. In the Templates pane, select Event Receiver and name it TestEventReceiver1.

    The SharePoint Customization Wizard appears.

  5. On the Choose Event Receivers page, select List Item Events as the event receiver type.

  6. Set the Event source item to CustomField1 - ListDefinition1.

  7. In the list of events to handle, check the box next to An item was added and then click Finish.

  8. 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 override void ItemAdded(SPItemEventProperties properties)
    {
        properties.ListItem["Patient Name"] = "Scott Brown";
        properties.ListItem.Update();
        base.ItemAdded(properties);
    }
    
    
    
  9. Press F5 to run the code and view the SharePoint site in the Web browser.

  10. 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.

  11. Click List in the List Tools tab at the top of the page and then click the Modify This View button on the ribbon.

  12. 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.

  13. 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.

  14. 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.

Community Additions

ADD
Show:
© 2014 Microsoft