This example shows how to add a simple event processor to prevent deleting list items from a list. This task includes two steps:
- Create event processing in Visual StudioProgram
- Add event handlers to WSS as feature
Create an event handler in vistual Studio
-
- In Visual Studio, clickFile->New->ProjectTo create a new project
-
In the Create Project dialog box, select the project typeVisual C #, Template SelectionClass Library, Enter in the Name boxDeletingeventhandlerAnd then click OK.
-
In solution manager, selectDeletingeventhandler, InProjectClick Add reference in the menu.
-
- In the Add reference dialog box, selectMicrosoft. SharePoint(Located in. NetLabel) and click OK.
- InCodeIn the editor, referenceMicrosoft. SharePointThe namespace is as follows: UsingMicrosoft. SharePoint;
-
- Change the class nameDeletingactionAnd make it inherit fromSpitemeventreceiverClass: Public ClassDeletingaction: spitemeventreceiver
Use the following code to override 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-clickDeletingeventhandlerNode, PointAttribute.
-
In the Properties dialog box, clickSignatureLabel, selectSign this component, ClickSelect a strongly-named key fileAnd then click<New...>.
-
In the create strong naming key dialog box, enterDeletingeventhandler. SNKAs the key file name, you can also specify a password for the key and click OK.
-
Use the shortcut key Ctrl + Shift + B to compile the project.
-
In the Visual Studio project folder, find\ Deletingeventhandler \ bin \ debug
Folder, drag and drop the deletingeventhandler. dll fileLocal_drive: \ windows \ assembly
To perform global DLL deployment.
Add event processors as feature
-
InLocal_drive:/Program files/common files/Microsoft shared/Web Server Extensions/12/template/Features
Create a folder namedDeletingeventhandler.
- Create a new feature. xml file in the folder to identify the feature. The content is as follows. The featuer scope is the website. < Feature Scope = "Web"
Title = "Deleting event handler"
ID = "Guid"
Xmlns = "Http://schemas.microsoft.com/sharepoint" >
< Elementmanifests >
< Elementmanifest Location = "Elements. xml" />
</ Elementmanifests >
</ Feature >
-
Replace the guid placeholder in the previous step. You can generate a new guid through guidgen.exe. The command line tool is located inLocal_drive: \ Program Files \ Microsoft Visual Studio 8
.
- Create an element file elements. xml file in the deletingeventhandler folder to identify the components, classes, and methods of the event handler. In this example, the event handler is applied to the list of all notifications on the website. < Elements Xmlns = "Http://schemas.microsoft.com/sharepoint" >
< Receivers Listtemplateid = "104" >
< Cycler >
< Name > Deletingeventhandler </ Name >
< Type > Itemdeleting </ Type >
< Sequencenumber > 10000 </ Sequencenumber >
< Assembly >
Deletingeventhandler, version = 1.0.0.0, culture = neutral, publickeytoken = a26b5449ac4a4cf3 </ Assembly >
< Class > Deletingeventhandler. deletingaction </ Class >
< Data > </ Data >
< Filter > </ Filter >
</ Cycler >
</ Receivers >
</ Elements >
You can use the following method to obtain the Public Key token of the component. FindLocal_drive: \ Windows \ assembly
Deletingeventhandler. dll file, right-click the file, and clickAttributeIn the Properties dialog boxAverageLabel, select and copy the token.
In the command line, enter the following command to install and activate feature, and then restart IIS (Microsoft Internet Information Services) for the change to take effect.
Stsadm - O installfeature - filename deletingeventhandler \ feature. XML
stsadm - O activatefeature - filename deletingeventhandler \ feature. XML - URL http: // server / site / subsite
iisreset