Export (0) Print
Expand All

How to: Create an Event Handler Feature

Windows SharePoint Services 3

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

This example shows how to add a simple event handler that prevents items from being deleted from a list. Two procedures are involved in this task:

  • Creating an event handler in Microsoft Visual Studio

  • Adding the event handler as a Feature in Windows SharePoint Services

To create the event handler in Visual Studio

  1. Create a new project in Visual Studio by clicking File, pointing to New, and then clicking Project.

  2. In the New Project dialog box, select Visual C# in the Project types box, select Class Library in the Templates box, type DeletingEventHandler in the Name box, and then click OK.

  3. In Solution Explorer, select DeletingEventHandler, and click Add Reference on the Project menu.

  4. In the Add Reference dialog box, select Microsoft.SharePoint on the .NET tab and then click OK.

  5. In the Code Editor, import the Microsoft.SharePoint namespace as follows.

    using Microsoft.SharePoint;
    
  6. Change the name of the class to DeletingAction and make it inherit from the SPItemEventReceiver class, as follows.

    public class DeletingAction : SPItemEventReceiver
    
  7. Add the following code within the class to override the ItemDeleting method.

    public override void ItemDeleting(SPItemEventProperties properties)
    {
        properties.Cancel = true;
        properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";
    }
    
  8. In Solution Explorer, right-click the DeletingEventHandler node, and then click Properties.

  9. In the Properties dialog box, click the Signing tab, select Sign the asembly, select Choose a strong name key file, and then click <New…>.

  10. In the Create Strong Name Key dialog box, type DeletingEventHandler.snk in the Key file name box, optionally specify a password for the key, and then click OK.

  11. To build the project, click Build Solution on the Build menu, or press CTRL+SHIFT+B.

  12. Find the \DeletingEventHandler\bin\Debug folder in the Visual Studio Projects folder, and drag the DeletingEventHandler.dll file to Local_Drive:\WINDOWS\assembly to place the DLL in the global assembly cache.

To add the event handler as a Windows SharePoint Services Feature

  1. Create a folder in Local_Drive:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES called DeletingEventHandler.

  2. Create a Feature.xml file in this folder like the following that identifies the Feature and its element manifest file and sets the Feature scope to Web site.

    <Feature Scope="Web" 
       Title="Deleting Event Handler" 
       Id="GUID" 
       xmlns="http://schemas.microsoft.com/sharepoint/">
       <ElementManifests>
          <ElementManifest Location="Elements.xml"/>
       </ElementManifests>
    </Feature>
    
  3. To replace the GUID placeholder in the previous Id attribute, generate a GUID by running guidgen.exe located in Local_Drive:\Program Files\Microsoft Visual Studio 8.

  4. Create an Elements.xml file in the DeletingEventHandler folder that identifies the assembly, class, and method to implement as the event handler. This example applies the event handler to all announcements lists of a site, as specified by the ListTemplateId attribute. For the IDs of other default Windows SharePoint Services list template types, see the Type attribute description of the ListTemplate element.

    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
       <Receivers ListTemplateId="104">
          <Receiver>
             <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>
          </Receiver>
       </Receivers>
    </Elements>
    
  5. To get the Public Key Token of the assembly, in Windows Explorer find the DeletingEventHandler.dll file in the Local_Drive:\WINDOWS\assembly, right-click the file, click Properties, and on the General tab of the Properties dialog box, select and copy the token.

  6. At a command prompt, navigate to \Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN on the local drive, and type each of the following commands to install the Feature in the deployment, activate the Feature on a specified subsite, and reset Microsoft Internet Information Services (IIS) so that the changes take effect:

    stsadm -o installfeature -filename DeletingEventHandler\Feature.xml
    
    stsadm -o activatefeature -filename DeletingEventHandler\Feature.xml -url http://Server/Site/Subsite
    
    iisreset
    
  7. Try to delete an item in an announcements list on the specified Web site to see the effects of the event handler Feature.

Show:
© 2014 Microsoft