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.
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.
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace EventHandlinginSP.EventReceiver1
{
public class EventReceiver1 : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
SPListItem _currentItem =
properties.ListItem;
_currentItem["Title"] = DateTime.Now.Year;
_currentItem.Update();
}
}
}
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 Custom List and add new item , will give you the result!
0 comments:
Post a Comment