Applies to: SharePoint Foundation 2010
Microsoft SharePoint Foundation provides classes for responding to Feature events, which allow you to trap and respond to an event that fires when a Feature is installed in the server farm, when a Feature is added to a new virtual server, or when a Feature is removed.
Some of these events are post events, meaning that they fire after the respective behavior has been committed and the Feature definition is created in the collection of Feature definitions for the farm.
It is not possible to cancel an installation or uninstall operation through Feature events.
Feature Event Classes
Microsoft.SharePoint.SPFeatureReceiver Base abstract class that can be overridden to implement the activation, deactivation, installation, uninstallation, or upgrade operations of a Feature.
Microsoft.SharePoint.SPFeatureReceiverProperties Class that provides access to event properties, such as the Feature instance that has been created, for the Feature definition or the parent object of the Feature.
Feature Provisioning Event Handlers
Feature provisioning handlers allow you to write specific code to handle various events that transpire in the lifecycle of a Feature. The SPFeatureReceiver class provides the following methods for handling events:
FeatureUpgrading(SPFeatureReceiverProperties, String, IDictionary<String, String>)
To implement provisioning event handlers, create a class that derives from SPFeatureReceiver and implements these methods. You are always passed the pointer to the parent object, so you can customize as needed. The assembly that contains a Feature receiver must be located in the global assembly cache (GAC). In the Feature.xml file, you can register a Feature receiver through the ReceiverAssembly and ReceiverClass attributes of the top-level Feature element, for example:
<Feature Id="5E88AE4E-2ECB-4CC5-B9EC-9019250F5BC8" Title="Knowledge Base Infrastructure" Scope="Site" ReceiverAssembly="MS.Samples.SharePoint.KnowledgeBase, Version=18.104.22.168, Culture=neutral, PublicKeyToken=d24e3c1752ded9ed" ReceiverClass="MS.Samples.SharePoint.KnowledgeBase.KnowledgeBaseInstaller" xmlns="https://schemas.microsoft.com/sharepoint/">
Any exceptions thrown during the activation process are communicated to the user, and the Feature is not marked as activated.
Write code that handles cases where a user attempts to force activation of your Feature even though it is already activated. For example, if an event handler in your Feature Receiver creates a list, verify that your code does not fail if the list already exists.