Partilhar via


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 can contain templates, pages, list definitions, event handlers, workflows, customizations, and other objects. The directory that contains the Feature can also contain other resource files such as pages, images, ECMAScript (JavaScript, JScript) files, or Cascading Style Sheets (CSS). Features can also be dependent upon another Feature that has been applied at a broader scope. For example, a Feature that is scoped at the site level can depend on a Feature that is scoped at the site collection level. However, a Feature that is activated at a higher scope cannot be dependent upon a Feature that is activated at a smaller scope.

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.

Microsoft.SharePoint Namespace

  • 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.

Microsoft.SharePoint.Administration Namespace

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="https://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.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="Ribbon.Library.Actions.AddAButton"
    Location="ViewToolbar"
    RegistrationId="101"
    RegistrationType="List"
    Title="Add a Ribbon Button">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
          Location="Ribbon.Library.Actions.Controls._children">
          <Button Id="Ribbon.Library.Actions.Controls.NewRibbonButton"
            Command="NewRibbonButtonCommand"
            Image16by16="/_layouts/images/FILMSTRP.GIF"
            Image32by32="/_layouts/images/PPEOPLE.GIF"
            LabelText="Hello World"
            TemplateAlias="o2" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler 
          Command="NewRibbonButtonCommand"
          CommandScript="javascript:alert('Hello, world');" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>

Building Block: Solutions

Building Block: SharePoint 2010 Content Types

Building Block: Lists and Document Libraries

More Information about Features

Using Features in SharePoint Foundation

Feature Events

Feature.xml Files

How to: Create a Simple Feature