Create a Custom List Level Event Receiver For Item Added To SharePoint 2013

This article explains how to create a custom list level event receiver for an item updated in SharePoint 2013. 

Step 1

Open Visual Studio 2013 and create an empty SharePoint Project.

Empty SharePoint Project

Step 2

Enter the site URL and select Deploy as a Farm solution and click Finish.

Enter site URL

Step 3

Open Solution Explorer and add a new item.

add new item

Step 4

Select event receiver and enter the title of the event receiver. Click the Add button.

Select event receiver

Step 5

Select List Item Events in the type of event receiver and select the custom list option from the event source. Then select "an item was added" in events and click Finish.

Select List Item Events

Step 6

The following screen will appear.

added in events

Step 7

Go to the Event receiver cs file and paste the following coding into it. Here I have an “EventReceiver” list with Title, DateTimeNow and Action Columns.
  1. public override void ItemAdded(SPItemEventProperties properties)  
  2. {  
  3.     base.ItemAdded(properties);  
  4.     using (SPWeb web = properties.OpenWeb())  
  5.     {  
  6.         try  
  7.         {  
  8.             SPListItem currentItem = properties.ListItem;  
  9.             currentItem["DateTimeNow"] = DateTime.Now.ToString();  
  10.             currentItem["Action"] = "Item Added";  
  11.             currentItem.Update();  
  12.         }  
  13.         catch (Exception ex)  
  14.         {  
  15.             throw ex;  
  16.         }  
  17.     }  
program code

Step 8

Since I am targeting to add the event receiver only to the “EventReceiver” list, the Elements.xml file requires the following change.

element
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
  3.   <!--<Receivers ListTemplateId="100">-->  
  4.     <Receivers ListUrl="Lists/EventReceiver">  
  5.       <Receiver>  
  6.         <Name>ListItemAddedItemAdded</Name>  
  7.         <Type>ItemAdded</Type>  
  8.         <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>  
  9.         <Class>EventReceiverItemAdded.ListItemAdded.ListItemAdded</Class>  
  10.         <SequenceNumber>10000</SequenceNumber>  
  11.       </Receiver>  
  12.   </Receivers>  
  13. </Elements>  
Step 9

Build and deploy the solution.

deploy

Step 10

Go to your custom list (EventReceiver) and add a new Item.

event receiver

Enter title

Here's the result:

results

Summary

In this article we have created a custom list level event receiver for an item added to SharePoint 2013.