Creating a Simple Event Handler for SharePoint – Part 2

12 08 2008

In the last post we created the dll for our event handler, we now need to deploy this event handler as a feature so we can use it on our SharePoint lists.

Create a blank visual studio solution, it does not matter which project type you use (i would recommend a class library project). In your new project, add a new folder called ‘DeletingEventHandlerFeature’.

Within this new folder, create two XML files – one called Feature.xml and the other called Elements.xml.

Your solution explorer window in Visual Studio should look like this:

The feature.xml is the xml file which defines the feature itself, open the feature file and add the following code:

<Feature Scope=”Web”

Title=”Deleting Event Handler”

Id=”ENTER A GUID HERE”

xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;

<ElementManifests>

<ElementManifest Location=”Elements.xml”/>

</ElementManifests>

</Feature> 

Be sure to enter a unique GUID into the ‘id’ attribute by generating one from within Visual Studio. The elements.xml file will call the dll we created earlier and will specify the type of list that the event handler will attach to. In order to tell the feature the details of the dll it is using, we need to get the assembly and class name of the dll we created in part 1, I would recommend a program called the .NET Reflector to do this – http://www.aisto.com/roeder/dotnet/.

Enter the following code into the elements.xml file, replacing the <assembly> and <class> with the details of your dll:

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/“>

<Receivers ListTemplateId=”101″>

<Receiver>

<Name>DeletingEventHandler</Name>

<Type>ItemDeleting</Type>

<SequenceNumber>10000</SequenceNumber>

<Assembly>DeletingEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=81323876ac0307e0</Assembly>

<Class>DeletingEventHandler.DeletingAction</Class>

<Data></Data>

<Filter></Filter>

</Receiver>

</Receivers>

</Elements>

In order to specify the correct list type to attach to, change the Receivers ListTemplateId accordingly, to see a full list of SharePoint list types, visit the following url: http://msdn.microsoft.com/en-us/library/ms431081.aspx.

Now we have created our feature, we must deploy the dll to the GAC (Global Assembly Cache) and install/activate our feature. Log on to your SharePoint web front end server and copy the dll created in Part 1 to ‘C:\Windows\Assembly’, this will install the dll into the GAC.

Copy the folder created in your visual studio project to the following folder on your SharePoint server: ‘C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\FEATURES’, you can use an XCOPY command at the project debug time if you wish.

To install the feature, you will need to use the SharePoint ‘STSADM’ admin tool, run this from the command line with the following switches:

To Install:

-o installfeature -name DeletingEventHandlerFeature

To Activate:

-o activatefeature -name DeletingEventHandlerFeature -url ‘Site URL Here’

Once you have run these commands, be sure to do an IISRESET, the event handler should now be attached to lists of the type which you specified in the elements file, it will also be attached for all future lists of that type that are created.

🙂

Advertisements

Actions

Information

3 responses

12 01 2010
DJ

Hi, Thanks a lot for this great article.

Im quite new to Sharepoint .I tried this . Im getting an error while instaling the feature in Feature.xml. The data at the root level is invalid in Feature.xml. Im not sure how to proceed with this.

12 01 2010
DJ

Hi, Thanks a lot for this great article.

Im quite new to Sharepoint .I tried this . Im getting an error while instaling the feature in Feature.xml. The data at the root level is invalid in Feature.xml. Im not sure how to proceed with this….pls help!

6 03 2010
Jey

Hi DJ,
I also faced the same problem. It might be a copy paste issue. Remove the double quotes in the tag and type it on your own. This solved my issue. It may be help full to you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: