Skip to content


Add an Event Handler (Feature Receiver) to a SharePoint Feature

An overview of what a feature is and how to create one can be found here

This post will show how to add an Event Handler to a feature. I will assume you have a feature up and running in Visual Studio. A download to a zipped copy of my project will be posted at the end.

Event Handlers are useful since they contain custom code you wish to run when a feature event executes. There are 4 events that you can respond to:

FeatureInstalled: Right after feature is installed.
FeatureActivated: Right after feature activation through “Site Settings” or otherwise.
FeatureDeactivated: Right after feature deactivation through “Site Settings” or otherwise.
FeatureUninstalled: Before feature is uninstalled.

This enables you to do things that can’t be done through element files.

This example will create a new list and add a couple items to it. This can be done through element files, but I’ll just use it as an example.

1: The Project
Make sure you know what namespace and assembly name for your project is, otherwise you will probably get in trouble later on. I set both to CodeJourney here.
We will deploy the DLL in GAC (Global Assembly Cache) and to do that it needs to be signed. Do that by rightclicking the project in “Solution Explorer”, click “Signing” tab, and select “Sign the assembly”. Next click the empty dropdownbox and select ““. Give it a name and uncheck the checkbox, so you don’t need to password protect it.

2: Add a reference to Microsoft.SharePoint
Make sure you have a reference to Microsoft.SharePoint.dll. If not Rightclick “References”, choose “Add Reference” and under .NET tab choose “Windows SharePoint Services” which is the SharePoint main dll.

3: Create Feature Receiver
In your Visual Studio project containing a ready to go feature, add a new class file.
We will name it FeatureReceiver.cs To create a FeatureReceiver we will inherit from SPFeatureReceiver and override 4 methods.
Add whichever code you wish for your feature receiver to have.

01
using System;
02
using System.Text;
03
using Microsoft.SharePoint;
04
 
05
namespace FeatureEventHandler
06
{
07
  public class FeatureReceiver : SPFeatureReceiver 
08
  {
09
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
10
    {
11
      SPSite site = (SPSite)properties.Feature.Parent;
12
 
13
      SPWeb web = site.OpenWeb();
14
      Guid guid = web.Lists.Add("MyList", "List added from EventReceiver", SPListTemplateType.GenericList);
15
      SPList list = web.Lists[guid];
16
      list.OnQuickLaunch = true;
17
 
18
      list.Fields.Add("MyField", SPFieldType.Text, false);
19
      SPListItem listitem = list.Items.Add();
20
      listitem["Title"] = "New Item";
21
      listitem["MyField"] = "Bla bla";
22
      listitem.Update();
23
      list.Update();
24
 
25
    }
26
 
27
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
28
    {
29
      SPSite site = (SPSite)properties.Feature.Parent;
30
      SPWeb web = site.OpenWeb();
31
      web.Lists["MyList"].Delete();
32
      web.Update();
33
    }
34
 
35
    public override void FeatureInstalled(SPFeatureReceiverProperties properties){}
36
    public override void FeatureUninstalling(SPFeatureReceiverProperties properties){}
37
  }
38
}
39
 
40

4: Edit install.bat to add dll to GAC
For development testing gacutil.exe is often used to copy files to GAC. Files can’t simply be copied to c:\windows\assembly since it’s a special folder and you will get an error if you try. Gacutil is not automatically installed on your computer, so install the SDK for your .NET version.

In case you don’t want to download the SDK, just simply drag and drop the DLL file from your bin\debug directory in your project to C:\Windows\Assembly.

If you have odd troubles with encoding using .bat files in Visual Studio 2008 read this.

For the Windows Server 2008 – .NET 3.5 SDK use these lines:
@SET GACUTIL=”C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\gacutil.exe”
%GACUTIL% -if bin\Debug\CodeJourney.dll

Otherwise use these:
@SET GACUTIL=”c:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe”
%GACUTIL% -if bin\Debug\CodeJourney.dll

5: Edit feature.xml
If you try and compile now nothing much will happend after you activate the feature. We haven’t told SharePoint that the feature has a feature reciever yet. This is done in the feature.xml file.

Add the ReceiverAssembly and ReceiverClass to the XML file. Mine looks like this now:

<feature>
         Id="7EAEA45D-219C-418e-9719-4812F2A1FBA1"
         Title="Code-Journey - Feature Event Handler"
         Description="A Feature with an Event Handler that creates and deletes a list."
         Hidden="False"
         Scope="Site"
         ReceiverAssembly="CodeJourney, Version 1.0.0.0, Culture=neutral,    PublicKeyToken=664896a75ad26d79"
         ReceiverClass="FeatureEventHandler.FeatureReceiver"   
         >
</feature>

The 4-part name of the assembly can be found using Red Gate’s .NET Reflector which is free.

Build your project now, and go to Site Settings and activate the feature. Once activated you can find the newly created list in the QuickLaunch.

If something does not work, then it’s often errors in the ReceiverAssembly and ReceiverClass lines in the feature.xml, or bad code in the FeatureReceiver itself. Sometimes looking in the logfiles in 12\LOGS folder helps.

You can download the complete project here: FeatureEventHandler (85).

DeliciousDiggTwitterFacebookLinkedInTechNetStumbleUponNewsVineTechnorati FavoritesSlashdotMSDNShare

Related posts:

  1. How to create a new feature in sharepoint using Visual Studio
  2. Make a SharePoint Application Page with CodeBehind
  3. Create Simple SharePoint Hello World WebPart And Add It To a Web Part Page
  4. Feature ’75a0fea7-c966-4e74-b74e-8f77e7bae175′ is not installed in this farm, and can not be added to this scope.
  5. How To Add A UserControl To A Web Part In SharePoint 2007

Posted in SharePoint 2007.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.



Page optimized by WP Minify WordPress Plugin