Create an event handler to prevent items from deleting item from SharePoint list using Visual Studio 2010

Here in this SharePoint 2010 tutorial, I will explain, how to create an event handler to prevent items from deleting an item from SharePoint list using Visual Studio 2010.

Here we will create and attach the event handler to the SharePoint 2010 list. When a user tries to delete an item, it will display an error message and it will prevent the user from deleting the SharePoint 2010 list item.

Create Event Handler for SharePoint 2010 List using Visual Studio 2010

Now we will create the event handler for SharePoint 2010 list using visual studio 2010.

– Open Microsoft Visual Studio 2010 and then go to File -> New -> Project.

– Then from the New Project dialog box, From Visual C# template, Select SharePoint and then 2010 and from there select Event Receiver project template, then give a name DeleteEventReceiver and click on OK as shown in the fig below.

create event handler sharepoint 2013 using visual studio 2010

create event handler SharePoint 2010 using visual studio 2010

– Then in the SharePoint Customization Wizard, Give a local deployment URL and choose to Deploy as a sandboxed solution. Click Next.

– Then in the Next dialog box Choose Event Receivers Settings.

Select List Item Events in What type of event receiver do you want.

Then Select the List name in the What item should be the event source? dropdown. You can select the Task list or Announcement list etc.

Choose the An item is being deleted option in the Handle the following events list. Click Finish as shown in the fig below:

sharepoint event receiver

sharepoint event receiver

Then it will open the visual studio EventReceiver1 class file. Then override the ItemDeleting event handler.

public override void ItemDeleting(SPItemEventProperties properties)
{
base.ItemDeleting(properties);
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = “You could not delete the list item !!!”;
}

The full code will look like below:

sharepoint 2010 event handler example

sharepoint 2010 event handler example

Now deploy the solution and try to delete an item from the Task list in SharePoint 2010, it will show the error as shown in the fig below:

create event handler sharepoint 2013 using visual studio 2015

create event handler sharepoint 2010 using visual studio 2010

You may like following SharePoint event receivers tutorials:

Hope this article will be helpful to know how to create an event handler for SharePoint 2010 using visual studio 2010.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

>