Restrict a user to delete from a SharePoint List using Item Deleting Event Handler

No.of Views2057
Bookmarked0 times
Downloads 
Votes0
By  Ravish Kumar Sindhwani   On  18 Jun 2010 09:06:51
Tag : SharePoint , Development and Programming
In this article we will see that how to use item deleting event to restrict a user so that he will not be able to delete the item of a sharepoint list.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Sometimes we are in a situation that we need to restrict a user to delete a particular item from a sharepoint list, but how to restrict them.

There are two ways of doing that

  • First method is to give Item Level Permission. But the problem with this method is: Suppose one user added two items and wanted to delete one of them but we have some business logic that he should not be able to delete that particular item than how to restrict him for that particular item and allowed to delete another item.
  • Second method is to write an Event handler.

Step1

Open visual studio create one class library project. 

Image Loading

Step2

Add the reference for Microsoft.Sharepoint.dll as shown below.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

 Image Loading

Step3

To use the ItemDeleting event we need to inherit our class from SPItemEventReciever class as shown below.

public class Class1 :SPItemEventReceiver

Step4

Next step is to override ItemDeleting function as shown below.

public override void ItemDeleting(SPItemEventProperties properties)
        {
            SPListItem Item = properties.ListItem;if (Item["Vendor Name"] != null)
            {if (Item["Vendor Name"].ToString() == "Ravish")
                {//For canciling the delete operationproperties.Cancel = true;//Show error message to userproperties.ErrorMessage = "Can't Delete";

                }
            }
        }

Step5

To increase the trust level we need to sign the assembly created. To sign the assembly go to property page and check the box Sign the assembly as shown below. 

Image Loading

Step6

After that build the solution once again. Your event handler dll is ready. Now next step is to register the event handler to sharepoint list. To register the event handler follow the below mentioned steps.

Register Event Handler

There are so many ways to register an event. Here we are using U2U Event Handler Explorer to register the event to sharepoint list.

Step1

Place the signed DLL file in GAC.

Step2

Open U2U event handler explorer and explore the URL as shown in below screenshot.

Step2.1

Explore to the List on which you want to register event. In this case it is Vendor. 

Image Loading

Step3

First Click on Vendor list and after that click on Load Assembly and add the browse to the event handler DLL.

Step4

Click on the Class DropDown list and select the class name that we have given  in our class library.

Step5

Give a sequence number. The number should be greater than 10000. You can give any random number.

Step6

Click on Add Handler button and do IISRESET.

Your event handler has been registered.Now click on item which you want to delete as shown in the below screen shot.  

Image Loading

After click on delete you will get the below screen with the error message ‘Can’t Delete’ as specified in our code because Vendor name was “Ravish” as you know we are checking this condition in our code that if the vendor name Is Ravish than set Property.Cancel to true. This will restrict the user to delete the list item with vendor name “Ravish”. 

Image Loading

In this way we can restrict the user to delete the List Item using event handler.

Code Snippet

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace ItemDeletingEventHandler
{public class Class1 :SPItemEventReceiver
    {public override void ItemDeleting(SPItemEventProperties properties)
        {
            SPListItem Item = properties.ListItem;if (Item["Vendor Name"] != null)
            {if (Item["Vendor Name"].ToString() == "Ravish")
                {//For canciling the delete operationproperties.Cancel = true;//Show message to userproperties.ErrorMessage = "Can't Delete";

                }
            }
        }
    }
}

Sample Project Source

Download source files -403 kb

 
Sign Up to vote for this article
 
About Author
 
Ravish Kumar Sindhwani
Occupation-Software Engineer
Company-
Member Type-Fresh
Location-India
Joined date-11 Jun 2010
Home Page-
Blog Page-
I born and brought up in Haryana and working as a software developer in Trivandrum. I am passionate for learning new Microsoft technologies and I believe if you are learning any new thing, just write it some where. You will never forget.
 
 
Other popularSectionarticles
Comments
By:RRaveenDate Of Posted:8/5/2010 10:57:04 AM
Thank you and Welcome
Dear Sarwa, Thank you for your feedback and great ,this credit go our great author Ravish, we are expecting more great article from Ravish. thank you
By:SarwaDate Of Posted:8/5/2010 2:53:27 AM
Very Good Article
This is really damn good. It helps me lot. Thank you so much!
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top