Saturday, March 8, 2014

SharePoint 2013 - Feature Receivers

Feature Receivers
A feature receiver is a class which can execute custom code through SharePoint object model whenever a particular feature life cycle related events occurs.

Feature Receiver Events
A feature receiver can trap the following events:
1) Feature Activation: Occurs when a feature has been activated
2) Feature Deactivating: Occurs while a feature is deactivating
3) Feature Installation: Occurs when a feature has been installed
4) Feature Uninstalling: Occurs while a feature is un-installing
5) Feature Upgrading: Occurs while a feature is upgrading

Feature Receiver Event Classes

1) Microsoft.SharePoint.SPFeatureReceiver
To implement feature receivers, you need to define a new class that inherits from the base abstract class, SPFeatureReceiver, defined in the Microsoft.SharePoint namespace.
public abstract class SPFeatureReceiver {
public SPFeatureReceiver();
public virtual void FeatureActivated(SPFeatureReceiverProperties properties);
public virtual void FeatureDeactivating(SPFeatureReceiverProperties properties);
public virtual void FeatureInstalled(SPFeatureReceiverProperties properties);
public virtual void FeatureUninstalling(SPFeatureReceiverProperties properties);
public virtual void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, IDictionary<string, string> parameters);}

2) Microsoft.SharePoint.SPFeatureReceiverProperties
Each virtual method accepts an argument of type SPFeatureReceiverProperties, which allows accessing information about the target feature, its definition, and the current site.
public sealed class SPFeatureReceiverProperties : Idisposable {
public SPFeatureDefinition Definition { get; internal set; }
public SPFeature Feature { get; }
public SPSite UserCodeSite { get; }}

Feature.Parent property
We can access the context of the feature through the Feature.Parent property of the current SPFeatureReceiverProperties argument.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
  SPSite site = properties.Feature.Parent as SPSite//Depending on the scope of your feature
}

Feature.Parent Scope
Depending on the scope of your feature, the Parent property could be:-
1) The whole farm (SPFarm),
2) A single web application (SPWebApplication),
3) A Site Collection (SPSite),
4) A single website (SPWeb).

Feature Receiver Reference
To create a feature receiver, you need to implement the receiver class, build its assembly, put it into the GAC, and declare the ReceiverAssembly and ReceiverClass attributes in a feature manifest XML file.

<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Version="1.0.0.0"
Title="My Web Part"
Description="This feature deploys a sample Web Part."
Id="c46c270e-e722-4aa0-82ba-b66c8dd61f4e"
ReceiverAssembly="MyApp.SP2010.MyFeature, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=b001133e0647953d"
ReceiverClass="MyApp.SP2010.MyFeature.MyWebPartEventReceiver"
Scope="Site">
    <ElementManifests>
        <ElementManifest Location="MyWebPart\Elements.xml" />
        <ElementFile Location="MyWebPart\MyWebPart.webpart" />
    </ElementManifests>
</Feature>

Adding a Feature Receiver
In Visual Studio 2012, go to the Solution Explorer and right-click a feature item within the Features folder of your SharePoint project to open the contextual menu. There, you can select the Add Event Receiver menu item, which will create all the plumbing code for you.

Hope this gives a brief overview about SharePoint 2013 Feature Receivers.  In the next post we will create a sample feature receiver in order to take a deeper look.

No comments:

Post a Comment