Introduction to SharePoint Features:

One of the major concepts introduced in WSS 3.0/MOSS 2007 is the “Feature”. Feature is a mechanism for deployment which drastically reduces the complexity of making site customizations and deploying upgrades on existing SharePoint sites.

Features can be used to package and deploy a set of SharePoint site elements such as pages, list types, list instances, site columns, content types, event handlers, master pages, page layouts, workflows, etc. we can also attach a piece of code with Features which executes when that Feature is activated/deactivated. 

SharePoint Feature is a set of XML files which are deployed in the web front end servers and can be bundled within SharePoint Site Definitions or can be individually activated in the SharePoint sites. All the features bundled in the Site Definition, are activated automatically when a new site is created using the respective site definition.

A feature can be developed to implement a specific functionality and installed in the SharePoint web front end server. Once a Feature is installed in the web front end server, site administrators can activate or deactivate the feature in a particular SharePoint site to plug or unplug that respectively functionality.

One can find all the available Features within a web front end server at “C:\ProgramFiles\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES”.

Features can be scoped to the Farm, Web Application, Site, and Web level depending on the purpose of the feature.

Capabilities provided by SharePoint Features: 

Following are the major capabilities provided by SharePoint Features:

  • Pluggable behavior for installing or uninstalling Features within a SharePoint deployment.
  • Pluggable behavior for activating or deactivating Features at a given scope within a SharePoint Deployment.
  • Defined scopes of Features helps in determining where the custom code will run.
  • Site administrators can activate/deactivate a Feature from SharePoint site front end.
  • Same SharePoint Feature can be reused across multiple SharePoint sites depending upon the requirement which improves the component reusability.

Each SharePoint Feature essentially has a feature.xml file which describes the title, description, scope, receiver assembly, associated element manifest files, and other attributes of the Feature. Other than the feature.xml file, a Feature may also have elements.xml file, image files, .aspx files, .master files, etc depending on the requirement and the functionality the respective feature is implementing.

Create a custom Feature:

Let’s take an example to add a custom master page and a page layout to a SharePoint site. Following steps needs to be followed to create a custom Feature to accomplish this requirement:

1. Navigate to the Features directory on your web server: 

C:\Program Files\Common Files\Microsoft Shared\web serverextensions\12\TEMPLATE\FEATURES

2. Create a new folder within the FEATURES folder and name it CustomPageLayouts. One can choose the name of new Feature of his/her choice.

3. Create a folder MasterPages within the CustomPageLayouts folder and paste all the custom master pages in this fiolder.

4. Create a folder PageLayouts within the CustomPageLayouts folder and paste all the custom page layouts in this fiolder.

5. Create two xml file and name them feature.xml and elements.xml respectively.

6. Open the feature.xml file and paste the following xml content:

<?xml version="1.0" encoding="utf-8" ?>

<Feature Id=" F62C96CF-79FD-44be-8882-E9BFBD199184"

    Title="Custom Master Pages and Page Layouts"

    Description="This feature deploys custom Master Pages and Page Layouts"

    Version="1.0.0.0"

    Scope="Site"

    Hidden=”false”

    xmlns="http://schemas.micræsoft.com/sharepoint/">

      <ElementManifests>

            <ElementManifest Location="elements.xml" />

      </ElementManifests>

</Feature>

Feature ID must be a unique GUID. One can create a unique GUID using Visual Studio Tools. Open Visual Studio and Click on “Tools -> Create GUID”.

7. Open the elements.xml file and paste the following xml content:

<?xml version="1.0" encoding="utf-8" ?>

<Elements xmlns="http://schemas.micræsoft.com/sharepoint/">
   <Module Name="CustomPageLayouts" Url="_catalogs/masterpage" 
                                   Path="PageLayoutsRootWebOnly="TRUE">
    <FileUrl="CustomPageLayout1.aspx" Type="GhostableInLibrary">
      <Property Name="Title" Value="Page Layout Title" />
    </File>
  </Module>
  <Module Name="CustomMasterPages" Url="_catalogs/masterpage" 
                                   Path="MasterPagesRootWebOnly="TRUE">
    <FileUrl="CustomMasterPage1.master" Type="GhostableInLibrary">
      <Property Name="MasterPageDescription" Value="Master page description" />
    </File>
  </Module> 

</Elements>

The above element manifest file containsthe description of one page layout “CustomPageLayout1.aspx” and one master page“CustomMasterPage.master”. Make sure these two files are present in the "PageLayouts" and "MasterPages" folder respectively. One can add more mater pagesand page layouts as per requirement.

(A better way to implement this feature is to make a copy of existing "PublishingLayouts" Feature and rename it to "CustomPageLayouts", and do the required modifications in the feature.xml and other files and folders)

8. Once all the files are saved and placed in the respective folders, the feature is ready to be installed on the web front end server. Before running the install feature command, reset the IIS (open the command prompt and run the IISRESET command).

9. Now open a Command Prompt and enter the following cd command  to change the directories to the BIN folder in the 12 hive so you can use STSADM.exe directly:

cd C:\Program Files\Common Files\MicrosoftShared\web server extensions\12\BIN

Now run the following STSADM command which installs the Feature on the web front end server:

stsadm -o installfeature -name CustomPageLayouts

10. Feature has been installed in the web front end server. Now its time to activate this Feature on a particular SharePoint site. We can activate this feature either using the STSADM command or using the SharePoint site front end:

To activate this Feature using STSADM command, one need to excute following STSADM command:

stsadm -o installfeature -name CustomPageLayouts –url “http://servername:port”

To activate Feature using SharePoint site front end, open the siteand navigate to the Site Collection Features settings page (Site Actions -> Site Settings -> Modify All Site Settings -> Site Collection Features). Locate the new Feature using its title and click on “Activate” button to activate this feature in the current SharePoint site.

11. Once the “CustomPageLayouts” feature has been activated on a SharePoint site, the associated master pages and page layouts will be deployed in the SharePoint site. One can verify this by opening the master page gallery (Site Actions -> Site Settings -> Modify All Site Settings -> Master pages) and check if these new master pages and page layouts are there or not.


Installing/Uninstalling and Activating/Deactivating Features:

After creating the Feature folder, you can install and activate the Feature through command-line operations of stsadm.exe, or through the object model. You can also activate a Feature through the user interface. Installing a Feature makes its definition and elements known throughout a server farm, and activating the Feature makes the feature available at a particular scope. Similarly deactivating the Feature makes the feature unavailable at a particular scope and uninstalling a feature removes its definition and elements from the server farm.

STSADM command to install a Feature:
stsadm -o installfeature -name FeatureFolderName

STSADM command to uninstall a Feature:
stsadm -o uninstallfeature -name FeatureFolderName

STSADM command to activate a Feature:
stsadm -o activatefeature -name FeatureFolderName -url http://server/site/subsite

STSADM command to deactivate a Feature:
stsadm -o deactivatefeature -name FeatureFolderName -url http://server/site/subsite 

"Site" and "Web" scoped Features can be also added within the "SiteFeatures" and "WebFeatures" elements respectively in the Onet.xml of a particular site definition. When a new site is created using this site definitions, all the all the added features will be activated automatically in the newly created site. But prerequisite for this is that all these features must be install on that server farm before creating the site.