This example shows how to add a simple event handler to prevent the deletion of list items from a list. This task consists of two steps:
To create an event handler in Visual Studio
Add an event handler as a feature to WSS
Create an event handler in Vistual Studio
Create a new project by clicking the file-> new-> project in Visual Studio
In the New Project dialog box, select the project type as Visual C #, select the class library for the template, enter Deletingeventhandler in the Name box, and then click OK.
In Solution Manager, select Deletingeventhandler and click Add Reference in the Project menu.
In the Add Reference dialog box, select Microsoft.SharePoint (located in the. NET tab) and click OK.
In the Code Editor, reference the Microsoft.SharePoint namespace as follows:
Using Microsoft.SharePoint;
Change the class name to Deletingaction and make it inherit from the Spitemeventreceiver class, as follows:
public class DeletingAction : SPItemEventReceiver
Use the following code to overwrite the Itemdeleting method of the class.
public override void ItemDeleting(SPItemEventPropertIEs properties)
{
properties.Cancel = true;
properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";
}
In Solution Manager, right-click the Deletingeventhandler node and point properties.
In the Properties dialog box, click on the Signature tab, select Sign the component, click Select a strong named key file, and then click < New ...
In the Create strong named Key dialog box, enter DELETINGEVENTHANDLER.SNK as the key file name, or you can assign a password to the key and click OK.
Compile the project with the shortcut key ctrl+shift+b.