Can you explain the broader steps for deploying/activating a feature?

 Posted by ArticlesMaint on 9/23/2009 | Category: SharePoint Interview questions | Views: 2893

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"
ReceiverAssembly="SharePointFeature, Version=, Culture=neutral, PublicKeyToken=af83741e324f585c"
xmlns="" >
<ElementManifest Location="ElementManifest.xml" />

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="">
Title="Display Custom Pages"
Description="This links helps to display Custom Page"
<UrlAction Url="_layouts/SimplePageCodeBehind.aspx"/>

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.Title = "This Site now has the custom page display";

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"];

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response