Create and Implement Event Receivers in SharePoint
SharePoint Event Receiver is designed to react to event fired by SharePoint modules. For example, Feature activation and deactivation can be used to trigger other events related to this feature. Often times event handlers developed for Feature events are called Feature Receivers. We’ll attempt to create such an event receiver that will change site title based on the Feature being activated.
First, we need to create a new class that inherits Microsoft.SharePoint.SPFeatureReceiver and implements all four events provided by the SPFeatureReceiver. This class then needs to be compiled into a signed assembly and deployed into GAC.
using System; using Microsoft.SharePoint; namespace MyFeatureReceiverClass { public class MyFeatureReceiver : SPFeatureReceiver { public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPWeb site = properties.Feature.Parent as SPWeb; site.Properties["MySiteTitle"] = site.Title; site.Properties.Update(); site.Title = DateTime.Now.ToString(); site.Update(); } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { SPWeb site = properties.Feature.Parent as SPWeb; site.Title = site.Properties["SiteTitle"].ToString(); site.Update(); } public override void FeatureInstalled(SPFeatureReceiverProperties properties) { // … } public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { // … } } } Second, we need to configure feature.xml file in order to call on this method. This configuration looks like the following: