Creating Event Receiver in sharepoint 2010

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.
choosing event receiver settings

choosing event receiver settings

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″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
<Receivers ListUrl=”http://<<servername>>/sites/Home/Lists/Source”&gt;
<Receiver>
<Name>EventReceiver1ItemAdding</Name>
<Type>ItemAdding</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>EventHandlerDemo.EventReceiver1.EventReceiver1</Class>
<SequenceNumber>10000</SequenceNumber>
</Receiver>
<Receiver>
<Name>EventReceiver1ItemAdded</Name>
<Type>ItemAdded</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>EventHandlerDemo.EventReceiver1.EventReceiver1</Class>
<SequenceNumber>10000</SequenceNumber>
</Receiver>
</Receivers>
</Elements>

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.

Advertisements

2 thoughts on “Creating Event Receiver in sharepoint 2010

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s