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.
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.
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.
Note | 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:
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 HolidayTheme.cs. Populate the 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 HolidayTheme.cs file and associated HolidayTheme Feature folder, which contains the 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){} } }
To install and deploy the Feature event demonstrated above, you need to create a Feature.xml file and reference the Feature event assembly and class using the ReceiverAssembly and ReceiverClass attributes, as shown in the following code sample.
Note | 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="http://schemas.microsoft.com/sharepoint/" Id="" Title="Theme Manager" Description="Use this Feature to activate the Holiday Theme for a site" Version="1.0.0.0" Scope="Web" Hidden="FALSE" ReceiverAssembly="SharePoint, Version=1.0.0.0, 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.