Implementing Feature Events

You can use Feature events to manage a Feature life cycle by triggering events during Feature activation and Feature deactivation. Through Feature events, you can more succinctly manage the Feature life cycle by choosing to have certain events occur throughout each stage of Feature deployment, such as when one of the following actions are attempted for a Feature:

  • Installed

  • Activated

  • Deactivated

  • Uninstalled

For example, you might have dates of company events throughout the year that you want deployed to your SharePoint sites, but only for the period leading up to each event. You can create a Feature and use Feature events to deploy event details to sites or a specific site when that Feature is activated, and then remove those event details when the Feature is deactivated. Or perhaps you want the ability to turn on and turn off access to particular sites, such as granting a particular group of users access to a site for a given period, or maybe you want to activate a particular workflow but only at certain times during a month or a year. You can accomplish all of these tasks using Feature events.

Instantiating the SPFeatureReceiver Class Provisioning Callouts

Feature event provisioning callouts derive from the Microsoft.SharePoint.SPFeatureReceiver class. There are four provisioning callouts:

  • FeatureInstalled

  • FeatureUninstalling

  • FeatureActivated

  • FeatureDeactivating

The sample code shown in the "Creating the Feature Event File" section below shows a class that has been derived from the SPFeatureReceiver and the FeatureActivated and FeatureDeactivating callouts provisioned.

Creating the Feature Event File

This sample assumes two custom themes have been created: Holiday and Corporate. Using the FeatureActivated callout, the Holiday theme is applied to the targeted site or sites when the Feature is activated;using the FeatureDeactivating callout, the Corporate theme is applied when the Feature is deactivated.


You can just as easily use the default themes to demonstrate the functionality shown in this example. Default themes can be found at the following location:

  • %SystemDrive%\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\THEMES

Choose two default themes and replace each Holiday and Corporate with the default themes.

To create the sample below, create a new class file in your Visual Studio project and name the class file image from book HolidayTheme.cs. Populate the image from book HolidayTheme.cs file with the code shown below.

On the CD 

The code in this example is included on the book's CD in the Visual Studio SharePoint solution. This includes the image from book HolidayTheme.cs file and associated HolidayTheme Feature folder, which contains the image from book Feature.xml file.

 using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; namespace SharePoint {     class HolidayTheme : SPFeatureReceiver     {         public override void FeatureInstalled(SPFeatureReceiverProperties properties){}3         public override void FeatureActivated(SPFeatureReceiverProperties properties)         {             SPWeb site = (SPWeb)properties.Feature.Parent;             site.ApplyTheme("Holiday");             site.Update();         }         public override void FeatureDeactivating(SPFeatureReceiverProperties properties)         {             SPWeb site = properties.Feature.Parent as SPWeb;             site.ApplyTheme("Corporate");             site.Update();         }         public override void FeatureUninstalling(SPFeatureReceiverProperties properties){}     } } 

Configuring the Feature.xml File for Feature Event

To install and deploy the Feature event demonstrated above, you need to create a image from book Feature.xml file and reference the Feature event assembly and class using the ReceiverAssembly and ReceiverClass attributes, as shown in the following code sample.


This sample assumes you have created the HolidayTheme class file as part of the SharePoint project and deployed or updated the project DLL to the GAC. See the "Creating an Event Handler Feature" section for details on how to build and deploy project DLLs.

 <?xml version="1.0" encoding="utf-8" ?> <Feature xmlns=""   Id=""   Title="Theme Manager"   Description="Use this Feature to activate the Holiday Theme for a site"   Version=""   Scope="Web"   Hidden="FALSE"   ReceiverAssembly="SharePoint, Version=, Culture=neutral, PublicKeyToken=ad7f6a147689147c"   Receiver   ImageUrl="" > </Feature> 

Copy the Feature to the Features directory on your SharePoint Web front-end server, install the Feature, and then activate and deactivate the Feature to trigger each of the Feature events. This Feature has been scoped for Web, which means you can activate and deactivate the Feature on sites throughout your SharePoint site collection.

Microsoft Office Sharepoint Server 2007 Administrator's Companion
MicrosoftВ® Office SharePointВ® Server 2007 Administrators Companion
ISBN: 0735622825
EAN: 2147483647
Year: 2004
Pages: 299

Similar book on Amazon
Microsoft Office SharePoint Server 2007 Best Practices
Microsoft Office SharePoint Server 2007 Best Practices
Microsoft SharePoint 2010 Administrator's Companion
Microsoft SharePoint 2010 Administrator's Companion
Professional SharePoint 2010 Administration
Professional SharePoint 2010 Administration
Inside Microsoft  Office SharePoint  Server 2007
Inside Microsoft Office SharePoint Server 2007 © 2008-2017.
If you may any questions please contact us: