Building Block: Features
Applies to: SharePoint Foundation 2010
A Feature is a container that includes one or more extensions to functionality in Microsoft SharePoint Foundation. A Feature contains a Feature.xml file and one or more element files. These XML files are also known as Feature definitions.
Features also contain Feature receivers. These are assemblies that contain code that runs when a Feature is installed, uninstalled, activated, deactivated, or upgraded. For more information about feature receivers, see Feature Events.
Server Farm – The Feature will be available in the server farm.
Web Application – The Feature will be available in the Web application.
Site Collection – The Feature will be available in a site collection.
Site – The Feature will be available in a single site.
Object Model for Features
Features can be manipulated by using the SharePoint Foundation object model. This includes finding information about a Feature, retrieving a list of installed Features, and determining activation dependencies. The following classes can be used to work with Features by using the object model.
- SPFeature – An object that represents a Feature. The collection of Features is stored in an SPFeatureCollection object. The SPFeatureCollection can be accessed by using the Features property on the SPWebService, SPWebApplication, SPSite, and SPWeb objects. If a Feature appears in the collection, it has been activated at the specified scope.
- SPFeatureProperty – An object that represents a single property on an SPFeature object. The collection of properties is stored in an SPFeaturePropertyCollection object. The SPFeaturePropertyCollection object can be accessed by using the Properties property of the SPFeature object.
- SPFeatureScope – An enumeration of the different scopes that can be specified for a Feature. These include Farm, WebApplication, Site, and Web.
- SPFeatureDependency – An object that represents a Feature that is depended upon by another Feature. The collection of Features that are depended upon by another Feature is stored in an SPFeatureDependencyCollection object. The SPFeatureDependencyCollection object can be accessed by using the ActivationDependencies property of the SPFeatureDefinition object.
- SPFeatureDefinition – An object that represents the base definition of a Feature including the name, scope, ID, and version. The collection of Feature definitions is stored in an SPFeatureDefinitionCollection object. The SPFeatureDefinitionCollection object can be accessed by using the FeatureDefinitions property of the SPFarm or SPSite objects.
- SPElementDefinition – An object that represents an element that is provisioned when a Feature is activated or used. The collection of element definitions is stored in an SPElementDefinitionCollection object. The SPElementDefinitionCollection object can be accessed by using the GetElementDefinitions(CultureInfo) method of the SPFeatureDefinition object.
XML Used for Features
Features are primarily constructed by using two XML files—a Feature.xml file and a Feature elements file. These two files define the scope, dependencies, and any related files that will be deployed to the front-end Web server.
Feature Element Manifest File
A Feature.xml file is used to specify the location of assemblies, files, dependencies, or properties that support the Feature. The ElementManifest Element (Feature) element points to the accompanying Feature manifest file that defines the feature. For more information about the values that are used for attributes, see Feature.xml Files. A basic Feature.xml file has the following form.
<Feature Title="Feature Title" Scope="FeatureScope" Id="GUID" xmlns="http://schemas.microsoft.com/sharepoint/"> <ElementManifests> <ElementManifest Location="FeatureManifestFileName.xml" /> </ElementManifests> </Feature>
Feature Element File
A Feature element file can have any name. When defining your Feature.xml file, you must reference the filename in the ElementManifest Element (Feature) element. This file can contain a CustomAction Element element, user interface customizations, Ribbon customizations, and many other customizations. For more information about the structure of the file, see Feature Schemas. A sample Feature element file has the following form.