In order to understand the feature concepts lets deploy the simple application page i.e. ‘SimplePageCodeBehind.aspx’ as feature. So when the user activates this feature he will be able to browse the ‘SimplePageCodeBehind.aspx’.
Step 1:- Let’s create a project ‘SharePointFeature’. You can find the source code of the same attached with this article. So below is the project tree which has the two XML files and a class file which will process the four events i.e. ‘FeatureInstalled’,’FeaturesUnInstalling’ , ‘FeatureActivated’ and ‘FeatureDeactivating’.
Let’s understand these three files first.
Feature.XML :- This is the most important file because it helps SharePoint identify the feature. All features in SharePoint are identified by the GUID key.
<Feature Id="48DEF2C4-33F9-4885-B0DE-6FE82E9FDCD8"
Title="Go to Custom Pages"
Description="This features enables us to goto Custom Page"
Scope="Web"
Hidden="FALSE"
ImageUrl="menuprofile.gif"
ReceiverAssembly="SharePointFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=af83741e324f585c"
ReceiverClass="SharePointFeature.clsFeatureReceiver"
xmlns="http://schemas.microsoft.com/sharepoint/" >
<ElementManifests>
<ElementManifest Location="ElementManifest.xml" />
</ElementManifests>
</Feature>
To generate a new GUID click on Tools à Create GUID and click ‘New GUID’. Tools menu you will get from within the IDE. We have marked the GUID value which you need to copy and paste in the ‘feature.xml ‘file.
Other than feature description and title there are two important things in the XML file. The first it points towards some other XML file and second it points to an assembly which captures events.
ElementManifest.XML file :- ElementManifest.xml file actually specifies how the implementation will look like. There are two important points to be noted for the ElementManiFest.XML file. The custom action tag specifies on which control the feature will be activated. The control we are targeting at this moment is the ‘SiteActionsToolBar’. This tool bar is the one which you see on the right hand side corner of your SharePoint portal. There is also a URLaction property which specifies which URL it redirect to.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="SiteActionsToolbar"
GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="10"
Title="Display Custom Pages"
Description="This links helps to display Custom Page"
ImageUrl="_layouts/SharePoint2/menuprofile.gif">
<UrlAction Url="_layouts/SimplePageCodeBehind.aspx"/>
</CustomAction>
</Elements>
In other words ‘ElementManifest.xml’ specifies the location of the feature and which page it should redirect to.
FeatureReceiver.cs :- This class listens and implements custom actions of the feature.
We need to first refer the share point namespace as shown in the below code snippet.
using System;
using System.Collections.Generic;
using System.Text;
// Refer the SharePoint namespace
using Microsoft.SharePoint;
We need to implement the ‘SPFeatureReceiver’ class and implement all the events.
namespace SharePointFeature
{
// Inherit from the 'SPFeatureReceiver" class
public class clsFeatureReceiver : SPFeatureReceiver
{
// Implement the four events of SPFeatureReceiver class
public override void FeatureInstalled(SPFeatureReceiverProperties properties){}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { }
// This event fires when the feature is activated
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
....
....
....
....
}
// This event fires when the feature is deactivated
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
....
....
....
...
}
}
}
As a sample in the ‘FeatureActivated’ event we have set the description and title of the website.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// get the object of SharePoint Web
SPWeb site = (SPWeb)properties.Feature.Parent;
// Set the description ,properties , titile and update the SpWeb object
site.Description = "Click on the SiteActions to See how the custom page display";
site.Properties["OriginalTitle"] = "Display CustomPage";
site.Properties.Update();
site.Title = "This Site now has the custom page display";
site.Update();
}
In ‘FeatureDeactivating’ we have reverted back the title and description.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
// Get hold of the SharePoint web object and reset back the values
SPWeb site = (SPWeb)properties.Feature.Parent;
site.Description = "Custom Page display is disabled";
site.Title = site.Properties["OriginalTitle"];
site.Update();
}