Creating a SharePoint Custom Event Handler as a feature – Basics

I’ll review the basic steps on creating a SharePoint custom event handler.

You begin by opening up Microsoft Visual Studio 2005 and create a new class project. Reference the Microsoft.SharePoint assembly and then rename the default.cs class to the name that you want to give to your event handler. Also, remember to add your using statement for Microsoft.SharePoint.

Depending on which type of event handler you are wanting to write, will determine the proper SharePoint class to derive from. A breakdown of the available SharePoint classes to derive from are as follows:

  • List Items – SPItemEventReceiver
  • List Columns – SPListEventReceiver
  • Site – SPWebEventReceiver
  • Email – SPEmailEventReceiver

At this point, you need to make a decision on whether you want to use Asynchronous or Synchronous events in your event handler. In order to determine which one you want, first decide if you want the event to fire before or after the event.

Asynchronous calls occur after the event, and do not block any code being executed in SharePoint. On the flipside, synchronous calls occur before the event, will block code being executed in SharePoint until your custom event handler is completed.

For a list of the available methods for each class, refer to Microsoft’s site:

SPItemEventReceiver Methods

SPListEventReceiver Methods

SPWebEventReceiver Methods

SPEmailEventReceiver Methods

Once you make your decision on what type of event receiver and also what type of method call you will be using, write out the override method . I will discuss further on manipulating particular methods.

Creating a Strong Name Key
After completing your method, you will need to sign the assembly. You can do this by going into solution explorer and right clicking your project and selecting properties. Then, click the Signing tab, then select Sign the Assembly, then select Choose a strong name key file and click “<New…>”. Type a name into the Key file name box (usually the name of your project). It needs to be in the format of xxxxxx.snk. You can set a password if you want, and click OK.

Creating Event Receiver to be installed as a Feature
Right click on your event handler project and select Add / New Folder. Name the folder as the same name as the project or similar. Add to blank XML files under the folder. Name the XML files as elements.xml and the other feature.xml.

In the feature.xml file, it should follow the following format:

   1: <Feature Scope="Web"

   2:     Title="My Event Handler"

   3:     Description ="Description goes here."

   4:     Id="F141E5EA-796E-4807-AB86-43F5DC560371"

   5:     xmlns="">

   6:     <ElementManifests>

   7:         <ElementManifest Location="elements.xml"/>

   8:     </ElementManifests>

   9: </Feature>

For further explanation, refer to the Microsoft site for all the available nodes.

   1: <Elements xmlns="">

   2:     <Receivers ListTemplateId="101">

   3:         <Receiver>

   4:             <Name>MyEventHandler</Name>

   5:             <Type>ItemAdded</Type>

   6:             <SequenceNumber>10000</SequenceNumber>

   7:             <Assembly>MyEventHandler, Version=, Culture=neutral, PublicKeyToken=1cd942f82aed8c82</Assembly>

   8:             <Class>MyEventHandler.EventHandler</Class>

   9:             <Data></Data>

  10:             <Filter></Filter>

  11:         </Receiver>

  12:     </Receivers>

  13: </Elements>

In the elements.xml, the ListTemplateId refers to which list to associate the event handler. 101 = document library. For a description of the available id, see here:

The <Type> node refers to the method that will be raised when the action takes place. For multiple methods, you will need multiple <Receiver> nodes. <SequenceNumber> node refers to the order of execution.

Add a text file to the root of the project and rename it to cab.ddf and input the following:

   1: ;** MyEventHandler.wsp **


   3: .Set CabinetNameTemplate= MyEventHandler.wsp

   4: .Set DiskDirectoryTemplate=CDROM

   5: .Set CompressionType=MSZIP

   6: .Set UniqueFiles="ON"

   7: .Set Cabinet=on

   8: .Set DiskDirectory1=Package

   9: ;**************************************************

  10: manifest.xml manifest.xml

  11: MyEventHandler\elements.xml MyEventHandler \elements.xml

  12: MyEventHandler \feature.xml MyEventHandler \feature.xml

  13: bin\debug\ MyEventHandler.dll MyEventHandler.dll

Then add another text file and rename that one to installer.bat. The following should be inputted:

   1: makecab /f cab.ddf

Add a new xml file to the root of the project and rename it to manifest.xml and input the following:

   1: <?xml version="1.0" encoding="utf-8" ?>

   2: <Solution xmlns="" SolutionId="BF2A762B-A158-456c-BCA5-38120E87D982">

   3:     <FeatureManifests>

   4:         <FeatureManifest Location="MyEventHandler\feature.xml"/>

   5:     </FeatureManifests>

   6:     <Assemblies>

   7:         <Assembly DeploymentTarget="GlobalAssemblyCache"  Location="MyEventHandler.dll">

   8:         </Assembly>

   9:     </Assemblies>

  10: </Solution>

Save all your work and build your project.

Leave a Reply

Your email address will not be published. Required fields are marked *