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.
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 added" in events and click Finish.
Step 6
The following screen will appear.
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.
- public override void ItemAdded(SPItemEventProperties properties)
- {
- base.ItemAdded(properties);
- using (SPWeb web = properties.OpenWeb())
- {
- try
- {
- SPListItem currentItem = properties.ListItem;
- currentItem["DateTimeNow"] = DateTime.Now.ToString();
- currentItem["Action"] = "Item Added";
- 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>ListItemAddedItemAdded</Name>
- <Type>ItemAdded</Type>
- <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
- <Class>EventReceiverItemAdded.ListItemAdded.ListItemAdded</Class>
- <SequenceNumber>10000</SequenceNumber>
- </Receiver>
- </Receivers>
- </Elements>
Step 9
Build and deploy the solution.
Step 10
Go to your custom list (EventReceiver) and add a new Item.
Here's the result:
Summary
In this article we have created a custom list level event receiver for an item added to SharePoint 2013.