Create a Custom List Level Event Receiver For Item Updated in SharePoint 2013

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

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.

select Deploy as a farm solution and click Finish

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.

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 updated" in the events and click Finish.

updated in events

Step 6

The following screen will appear.

event source

Step 7

Go to the Event receiver cs file and paste in the following coding.

  1. public override void ItemUpdated(SPItemEventProperties properties)  
  2. {  
  3.     base.ItemUpdated(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 Updated";  
  11.             currentItem.Update();  
  12.         }  
  13.         catch (Exception ex)  
  14.         {  
  15.             throw ex;  
  16.         }  
  17.     }  
  18. }   
Event receiver cs file

Step 8

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

elements
  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>ListItemUpdatedItemUpdated</Name>  
  7.         <Type>ItemUpdated</Type>  
  8.         <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>  
  9.         <Class>EventReceiverItemAdded.ListItemUpdated.ListItemUpdated</Class>  
  10.         <SequenceNumber>10000</SequenceNumber>  
  11.       </Receiver>  
  12.   </Receivers>  
  13. </Elements>  
Step 9

Build and deploy the solution.

deploy the solution

Step 10

Go to your custom list (EventReceiver) and update any old item.

Update any old item

edit item

Here I have changed the Title.

changed the Title

The following are the results:

results

Summary

In this article we saw how to create a custom list level event receiver for an item updated in SharePoint 2013.

 

Up Next
    Ebook Download
    View all
    Learn
    View all