Thursday, February 28, 2008

Adding Event Handlers for all Lists in SharePoint Using Features in WSS 3.0

To add event handler to all document libraries in the site:
Create a Class Library project "EventHandlerProject" in Microsoft Visual Studio 2005.
Add a class named "EventHandler".
Inherit this class from "SPItemEventReceiver".
Override the ItemDeleting method as follows:
public override void ItemDeleting(SPItemEventProperties properties)
{
base.ItemDeleting(properties);
properties.Cancel = true;
properties.ErrorMessage = "It should work now. its already too late";
}
Compile the above project and add the assembly in cache.
Create an elements.xml file and put the fillowing xml in it.
<elements xmlns="http://schemas.microsoft.com/sharepoint/">
<receivers listtemplateid="101">
<receiver>
<name>ItemDeleting</name>
<type>ItemDeleting</type>
<sequencenumber>400</sequencenumber>
<assembly>EventHandlerProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a1901tr121134ect</assembly>
<class>EventHandlerProject.EventHandlerClassName</class>
<data></data>
<filter></filter>
</receiver>
</receivers>
</elements>

Create a feature.xml and put following XML in it.
<feature scope="site" id="FBC1E9AD-741E-492a-84FB-491B7FCA1D79" title="Event Handler Feature" xmlns="http://schemas.microsoft.com/sharepoint/" description="My feature." hidden="false">
<elementmanifests>
<elementmanifest location="Elements.xml" />
</elementmanifests>
</feature>

No comments:

Post a Comment

Feel free to give constructive feedback or let me know if it was helpful.