How to: Create an Event Receiver for a Specific List Instance
A list instance event receiver responds to events that occur in any instance of a list definition. Although the event receiver template does not enable the targeting of a specific list instance, you can modify an event receiver that is scoped to a list definition to respond to events in a specific list instance.
To target a specific list instance, in the Elements.xml for the event receiver, replace ListTemplateId with ListUrl and add the URL of the list instance.
The following steps show how to modify a list item event receiver to respond only to events that occur in a custom announcements-list instance.
To modify an event receiver to respond to a specific list instance
Open the SharePoint site in a browser.
In the navigation pane, click Lists and then click Create.
In the Create dialog box, click Announcements, name the announcement TestAnnouncements, and then click Create.
In Visual Studio, create an event receiver project.
In the What type of event receiver do you want? list, select List Item Events.
You can also select any other kind of event receiver that scopes to a list definition, for example, List Email Events or List Workflow Events.
Select Announcements as the event source, click An item is being added in the events list, and then click Finish.
In Solution Explorer, under EventReceiver1, double-click Elements.xml to open it.
The event receiver currently references the Announcements list definition by using the line
Change this line to
This directs the event receiver to respond only to events that occur in the new TestAnnouncements announcements list that you just created. You can change the ListURL attribute to reference any list instance on the SharePoint server.
Open the code file for the event receiver and put a breakpoint in the ItemAdding method.
Press F5 to build and run the solution.
In SharePoint, click TestAnnouncements in the navigation pane.
Click the Add new announcement link.
Type a name for the announcement and then click Save.
Notice that the breakpoint is hit when the new item is added to the custom announcements list.
Press F5 to resume.
In the navigation pane, click Lists and then click Announcements.
Add a new announcement.
Notice that the event receiver does not trigger on the new announcement because the receiver is configured to respond only to events in the custom announcement list instance, TestAnnouncements.