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.
Step 2
Enter the site URL and select "Deploy as a farm solution" and click Finish.
Step 3
Open Solution Explorer and add a new item.
Step 4
Select event receiver and enter the title of the event receiver. Click the Add button.
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.
Step 6
The following screen will appear.
Step 7
Go to the Event receiver cs file and paste in the following coding.
- public override void ItemUpdated(SPItemEventProperties properties)
- {
- base.ItemUpdated(properties);
- using (SPWeb web = properties.OpenWeb())
- {
- try
- {
- SPListItem currentItem = properties.ListItem;
- currentItem["DateTimeNow"] = DateTime.Now.ToString();
- currentItem["Action"] = "Item Updated";
- currentItem.Update();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
Step 8Since I am targeting to add the event receiver only to the “EventReceiver” list, the Elements.xml file requires the following change.
- <?xml version="1.0" encoding="utf-8"?>
- <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
-
- <Receivers ListUrl="Lists/EventReceiver">
- <Receiver>
- <Name>ListItemUpdatedItemUpdated</Name>
- <Type>ItemUpdated</Type>
- <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
- <Class>EventReceiverItemAdded.ListItemUpdated.ListItemUpdated</Class>
- <SequenceNumber>10000</SequenceNumber>
- </Receiver>
- </Receivers>
- </Elements>
Step 9
Build and deploy the solution.
Step 10
Go to your custom list (EventReceiver) and update any old item.
Here I have changed the Title.
The following are the results:
Summary
In this article we saw how to create a custom list level event receiver for an item updated in SharePoint 2013.