SharePoint – Event Receiver to prevent documents from being deleted

Hello everyone. A few days ago I got a request to find a solution for the following problem: My customer has some Document Libraries where he stores important documents. These documents mustn´t be deleted since those documents are important contracts. So what can you do to prevent this?

Well there are some ways to achieve this: You can create a new Permission Level which does not allow the Delete action or you can create an Event Receiver which cancels the Delete action and throws an error message. I chose the second option since my customer and I both wanted to provide the user some information why his Delete action was canceled. (The first option would simply grey out the Delete action possibly resulting in users thinking there is something wrong with the software system.)

To create the Event Receiver do the following:

First open up Visual Studio on your Development machine and choose New Project. Select Event Receiver as your project template.


Next I chose Deploy as a sandboxed solution. (A sandboxed solution is enough for our planned actions.)


Next select Document Library from the Dropdown and check An item is being deleted as event.


And here is the code:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace SPDeletedReceiver.EventReceiver1
/// <summary>
/// List Item Events
/// </summary>
public class EventReceiver1 : SPItemEventReceiver
/// <summary>
/// An item is being deleted.
/// </summary>
public override void ItemDeleting(SPItemEventProperties properties)

using(SPWeb web = properties.Web)
SPList list = web.Lists["DoNotAllowDelete"];
SPQuery query = new SPQuery();
query.Query = string.Format("<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + properties.ListTitle + "</Value></Eq></Where>");
SPListItemCollection items = list.GetItems(query);

if(items.Count > 0)
properties.ErrorMessage = "You are not allowed to delete items.";
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.Cancel = true;

Code Explanation: The actions of the code are not very complex. What it simply does is check if the List Title of the List where the Delete action was triggered is listed in a Custom List called “DoNotAllowDelete”. If the List Title is listed the Delete action will be canceled. If not the Delete action will be executed.

So you are basically black listing some Document Libraries where the Delete action is forbidden for everyone.

To use the Event Receiver you need to upload the solution wsp file to your Solutions in your Site Collection and activate it. And you must activate the Event Receivers feature on the sites (Site features) where you want to use the solution.

Important: You also need to create a Custom List called “DoNotAllowDelete” on each Site you want to use the solution. And you need to enter the Library Titles of your black listed Document Libraries into the Title column of this list.

Here is an example picture of my “DoNotAllowDelete” list:


As you can see I black listed the Document Library “Documents”. If I now try to delete a document is the “Documents” library I will receive an error message like you can see below and the the document will not be deleted.


Well that´s it. I hope my post was interesting for you. See you next time.

As always you can download my code from here.


Tagged ,

One thought on “SharePoint – Event Receiver to prevent documents from being deleted

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: