PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

How to implement the SPFeatureReceiver Class to your feature?

Posted by PANVEGA on October 10, 2008

Introductions:

In this post I gonna show you how to implement the SPFEATURERECEIVER class in to your Sharepoint Solution project and give you an example how you can manipulate your Sharepoint Design when activating or deactivating your custom feature. You can use the receiver in almost every custom feature e.g. Webparts, Fieldtypes…

You will add an event handler that fires and executes code against the WSS object model each time the Feature is activated within a site. Catch the title from the Site, store the name in a Hashtable and give your site a new name when activating the feature. SharePoint remembers the original name and when you will deactivate the feature the original title will show up again.

General:

SharePoint features have the facility where you can write custom code that is triggered when certain events happen to features. It is a class called SPFeatureReceiver and when used, it traps events that are raised after Feature installation, uninstallation, activation, or deactivation action has been performed.

Steps how to develope a feature receiver:

  1. First, start by adding a project reference to Microsoft.SharePoint.dll (listed as Windows SharePoint® Services in Visual Studio’s Add Reference dialog).
  2. Implement the abstract SPFeatureReceiver Class into your class. You get 4 important receiver methods.
  3. In the Activating method you have to go to you parent scope
  4. store the original titlwe in the webs Hashtable
  5. Give your site a new name
  6. Update the properties and the web
  7. You can customize the installing or uninstalling methods with your custom code as well.

public class MyReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
//parent scope
using (SPWeb web = properties.Feature.Parent as SPWeb)
{
web.Properties[“My Title”] = web.Title;
web.Title = “Hello World”;
web.Properties.Update();
web.Update();
}
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
using (SPWeb web = properties.Feature.Parent as SPWeb)
{
web.Title = web.Properties[“My Title”];
web.Update();
}
}

public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{       }

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{        }

}

Now that you have implemented the Feature receiver class, the next step is to update the feature.xml file with two new attributes so that WSS knows that there is at least one event handler that should be fired whenever the Feature is activated or deactivated. This can be accomplished by adding the RecieverAssembly attribute and the ReceiverClass attribute as shown here

<Feature Id=”XXXXXXXXXXXXXXXXXXXXXXXX”
Title=”Getting Started with WSS Development”
Description=”This is my very first custom feature”
Version=”1.0.0.0″   Scope=”Web”
Hidden=”FALSE”  ImageUrl=”menuprofile.gif”
ReceiverAssembly=”
MyReceiver , Version=1.0.0.0, Culture=neutral,
PublicKeyToken=XXXXXXXXXXXXXXXXXXXX”
ReceiverClass=”Lab.MyReceiver ”  xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;

<ElementManifests>
<ElementManifest Location=”elements.xml” />
</ElementManifests>
</Feature>

Note:

Note that you can obtain the full 4-part assembly name using the Reflector utility.

Test your work by activating and deactivating the Feature. You should be able to see the site title changing back and forth each time you activate or deactivate the Feature.

Advertisements

2 Responses to “How to implement the SPFeatureReceiver Class to your feature?”

  1. Eric said

    Thanks a lot! I’ve been struggling with the attributes (ReceiverAssembly and ReceiverClass) to put in Feature.xml; what does the Elements.xml contain?

  2. Alexwebmaster said

    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru

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: