Sharepoint 2010 allows us to have EventReceiver on a specific list as against all the lists in the site. To achieve this all we have to do is configure the elements.xml file with details specific to the list on which we intend to trigger an event.
Steps for creating a sample event receiver in Sharepoint 2010
1. Create a new project of type Event Receiver in the sharepoint templates.
2. Select Farm based solutions and pass on an appropriate site URL.
3. Choose Event receiver settings
- In the Type of event receiver select “List Item Events”.
- Select the list on which you intend to have the event receiver. In my case I am doing it on my custom list named “source”.
- Select the events on which you intend to have receivers. I have selected one synchronous event “An item is being added” and an asynchronous event “An item was added”.
- Click Finish.
4. In the solution explorer, open the Elements.xml file which has got created. You will find a “Receivers” tag inside Elements. Note that by default “ListTemplateId” attribute is set to 100 which is used for generic lists.
5. Replace this attribute with ListUrl with value of the url set to the list on which we intent to have receiver.
6. Note that there would be two “Receiver” sections inside the “Receivers” section corresponding to the two events which we had selected earlier. Refer the Elements.xml file as below
<?xml version=”1.0″ encoding=”utf-8″?>
7. Open the EventReceiver.cs file from the solution explorer. You shall have events corresponding to the events which we found in the Elements.xml file.
8. You can have your custom code in these events as per your requirements.
9. For any additional event you can override the event in the code behind (.cs) file and make its respective entry as a new receiver in the Elements.xml file.
10. Thats to it. Your Event receiver is ready to be deployed.
11. Test it by creating an item in your designated list and one item in non designated list.