Microsoft Visual Studio 2010 provides a project type that enables you to build event receivers that perform actions before or after selected events on a Microsoft SharePoint 2010 site. We can create Event Receivers (Event Handlers) to take care of things for us when specific things happen. For example, we can subscribe to the "ItemAdded" event for a specific list, and have our custom code execute when the event fires.
In this article I am going to explain how to create a simple Item added event receiver for a custom list in SharePoint 2010. The event receiver will change the Title of the item to current year Time once it’s added to the list. We will create and deploy the solution as a farm and will run in debug mode.
· Open Visual Studio 2010 and create a new Project.
· In project types select Event Receiver and give it a name.
· Select .net framework 3.5.
Next in the SharePoint Customization Wizard, choose deploy as a farm solution and specify the site URL where you want the event handler to be deployed as shown in the below figure:
Next, in Event Receiver Settings dialog select the “List Item Events” as a type of event receiver and “Custom List” in the Item source dropdown. Select “An Item was Added” from Handle the following events as shown below:
Next, we will write some code to change the Title of an item to the current year when it is added to a custom list.
public class EventReceiver1 : SPItemEventReceiver
public override void ItemAdded(SPItemEventProperties properties)
SPListItem _currentItem = properties.ListItem;
_currentItem["Title"] = DateTime.Now.Year;
To build and deploy an application press “F5″.
Once press F5 the solution gets deployed to your site and your Visual studio project goes into a debug mode. Now we will create a new custom List “My List” and add a new item to test our handler.
Create a New Item -
Once you add the Item you will see the results.
Thanks for reading this article. I think after reading this article you can easily implement event handler for SharePoint sites.