SharePoint Feature Activation and Deactivation
SharePoint Feature Framework was introduced in WSS 3.0 release. Main objective of the SharePoint feature was to reduce code duplication, code reuse and synchronization. The Feature Framework is also used to deploy custom code for events, workflow templates and document converters. It can also be used to deploy master pages and page layouts within WSS 3.0.
One important characteristic of a feature is a need for this feature to be activated once this feature has been deployed. This activation and deactivation allows to enable or disable website elements via admin web based interface.
The SharePoint Feature is stored in SharePoint folder called FEATURES under main folder called “12”. There are total of 130 folders in the FEATURES folder each designated to take care of pre-built feature for out of the box SharePoint installation. In order to add custom built feature to a site, you need to add new folder (MyFeatureForSharePoint) into FEATURES folder with your feature file named “feature.xml”. This file contains a GUID for this feature, a title, a description and a scope.
Scope |
Description |
Web (SharePoint site) |
Only for specific site |
Site (site collection) |
Only for site collection and all other site collections within this site collection |
WebApplication |
Only WebApplication and sites within site collection |
Farm |
Only entire farm |
Example of the feature file is presented below:
<?xml version="1.0" encoding=" utf-8"?>
<Feature xmlns=" http://schemas.microsoft.com/sharepoint/"
Id="43TRCDEF-RT7C-4ABF-BGH5-DFRTFG66FGE7C"
Title="Feature Title"
Description="Feature Description."
Hidden="FALSE"
Scope="Web">
</Feature>
Placing feature.xml file into FEATURES folder accomplishes only half of a required task. The other task is to notify SharePoint of this newly installed feature. This is done with the help of STSADM.EXE command tool by running the following line:
stsadm.exe - o installfeature - name MyFeatureForSharePoint
Finally, activation of this feature is done with the help of the SharePoint Administrative panel. You need to navigate to Site Actions > Site Settings > Site Features. Your site feature should show up on the list of all available features. All you have to do next is to press Activate button.
If you want to create your feature with additional functionality, you may need to add element manifest and feature receiver.
Element manifest is defined in the element.xml file and can be added to the main feature.xml file as in the example below:
<?xml version="1.0" encoding=" utf-8"?>
<Feature xmlns=" http://schemas.microsoft.com/sharepoint/"
Id="43TRCDEF-RT7C-4ABF-BGH5-DFRTFG66FGE7C"
Title="Feature Title"
Description="Feature Description."
Hidden="FALSE"
Scope="Web">
<ElementManifests>
<ElementManifest Location=" elements.xml"/>
</ElementManifests>
</Feature>
There are several element types that exists in the SharePoint including the following:
Content type and content type bindings
Custom actions
Delegate controls
Document converters
Event registrations
Feature site template associations (stapling)
Field definitions (site columns)
List templates and instances
Modules
Workflow
Feature receivers are another piece of functionality that you can add to your feature. The SharePoint feature receiver is design to help trigger an event based on the feature action. For example, if you want to notify a user about new feature installation on the SharePoint you can use Events exposed by the Features.
There are four events that you can take into consideration while working with the SharePoint features:
FeatureInstalled
FeatureActivated
FeatureDeactivating
FeatureUninstalling