Reader Level:
ARTICLE

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

Posted by Ravish Sindhwani Articles | SharePoint June 03, 2010
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.
  • 0
  • 0
  • 12837
Download Files:
 

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.

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.

Step1. Open visual studio create one class library project.

image1.gif

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

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

image2.gif

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 operation
                    properties.Cancel = true;
                    //Show error message to user
                    properties.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.

image3.gif

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.

image4.gif

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.

image5.gif

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".

image6.gif

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

Code:

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 operation
                    properties.Cancel = true;
                    //Show message to user
                    properties.ErrorMessage = "Can't Delete";

                }
            }
        }
    }
}


Thank you!

Ravish
 

COMMENT USING

Trending up