IntroductionSometimes 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.
Step1Open visual studio create one class library project. Step2Add the reference for Microsoft.Sharepoint.dll as shown below. using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
 Step3To use the ItemDeleting event we need to inherit our class from SPItemEventReciever class as shown below. public class Class1 :SPItemEventReceiver Step4Next 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";
}
}
}
Step5To 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. Step6After 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 HandlerThere are so many ways to register an event. Here we are using U2U Event Handler Explorer to register the event to sharepoint list. Step1Place the signed DLL file in GAC. Step2Open 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. Step3First Click on Vendor list and after that click on Load Assembly and add the browse to the event handler DLL. Step4Click on the Class DropDown list and select the class name that we have given in our class library. Step5Give a sequence number. The number should be greater than 10000. You can give any random number. Step6Click 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. 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”. In this way we can restrict the user to delete the List Item using event handler. Code Snippetusing 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 SourceDownload source files -403 kb |