Create an event handler to prevent items from deleting item from SharePoint list using Visual Studio 2010
Check out Best Alternative to InfoPath -> Try Now
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.
– 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:
Then it will open the visual studio EventReceiver1 class file. Then override the ItemDeleting event handler.
public override void ItemDeleting(SPItemEventProperties properties)
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = “You could not delete the list item !!!”;
The full code will look like below:
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:
You may like following SharePoint event receivers tutorials:
- Event Receivers in SharePoint 2010
- BeforeProperties and AfterProperties in Event Receiver in SharePoint 2013
- List of SharePoint Event Model in SharePoint Server 2010
- Attach event receiver to list in SharePoint 2010
- Difference between workflows and event receivers in SharePoint 2013
- Remote Event Receivers in SharePoint online + Develop in Provider hosted Add-in using Visual Studio 2015 and Deploy to Microsoft Azure
Hope this article will be helpful to know how to create an event handler for SharePoint 2010 using visual studio 2010.
SharePoint Online FREE Training
JOIN a FREE SharePoint Video Course (3 Part Video Series)