原文:http://www.informit.com/articles/article.aspx?p=758415&seqNum=2
SharePoint includes a robust object model for working with Features that allows developers to enumerate installed and activated Features, to turn Features on and off, and to control the installation or removal of Features.
The object model for Features includes the following key classes:
- SPFeatureCollection/SPFeature—Refers to a Feature state at a given site hierarchy level. The presence of an SPFeature instance within a property of type SPFeatureCollection indicates that the Feature is active at that level.
- SPFeaturePropertyCollection/SPFeatureProperty—Represents a single property on a Feature or a collection of those properties.
- SPFeatureScope—Represents an enumeration of the possible scopes in which Features can be activated. Possible values are: Farm, WebApplication, Site, and Web.
- SPFeatureDefinition—Represents the basic definition of a Feature, including its name, scope, ID, and version. You can also store and retrieve properties of a Feature. Note that Feature properties apply globally to a single Feature definition, not to instances of Features activated throughout the farm.
- SPFeatureDependency—Represents a Feature upon which another Feature depends.
- SPElementDefinition—Represents a single element that will be provisioned when the Feature is activated.
Feature collections can be accessed from the following properties on their respective classes:
- Features on Microsoft.SharePoint.Administration.SPWebApplication
- Features on Microsoft.SharePoint.Administration.SPWebService
- FeatureDefinitions on Microsoft.SharePoint.Administration.SPFarm
- Features on Microsoft.SharePoint.SPSite
- Features on Microsoft.SharePoint.SPWeb
- ActivationDependencies on Microsoft.SharePoint.Administration.SPFeatureDefinition
The next few sections of this chapter provide many examples of programming with the Features portion of the SharePoint application programming interface (API).
Enumerating Features and Feature Definitions
It is important to recognize the difference between a Feature and a Feature definition. A Feature definition, as far as the object model is concerned, is an abstraction around the Feature manifest contained in a Feature directory in the Features directory. Feature definitions are installed at the farm (or server, if there is no farm) level.
A Feature is an instance of a Feature definition. Features can be activated or deactivated, and they exist at the various levels of scope such as the site or web level.
To enumerate the list of Feature definitions that are currently installed within a farm (which includes single-server farms and standalone servers, which are another form of single-server farms), you need to use an instance of the SPFarm class and access the FeatureDefinitions property. To enumerate the list of active Features on a given site, you need to enumerate the Features property on the appropriate SPWeb or SPSite class instance. You might be tempted to iterate through the collection contained in the Features property and look for something like an Active property. However, the only SPFeature instances that appear in the Features property are those features that are active in the current scope.
Creating an instance of the SPFarm class might seem a little tricky at first. Rather than obtaining it through a context provided by the Web Part manager or from a site uniform resource locator (URL), you need to pass a connection string that points to the farm's configuration database to the constructor. The connection string should look familiar to anyone with ADO.NET experience connecting to SQL server, because it is just a SQL server connection string.
The code in Listing 3.1 shows how to create an instance of the SPFarm class, create an instance of the SPSite class, and use the two of those to enumerate the list of installed Feature definitions and determine which of those definitions are active on the given site. This code is for a Windows Forms application that adds the name and enabled status of each Feature definition to a ListView control. If you plan to copy this code and test it, be sure to add a reference to the Microsoft.SharePoint.dll Assembly.
Listing 3.1 Enumerating Feature Definitions and Features
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using System.Windows.Forms; namespace FeatureEnumerator { public partial class Form1 : Form { private SPSite _rootCollection; public Form1() { InitializeComponent(); } private string GetFeatureEnabled(SPFeatureDefinition featureDefinition) { foreach (SPFeature feature in _rootCollection.Features) { if (feature.Definition.Id == featureDefinition.Id) return "Yes"; } return "No"; } private void button1_Click(object sender, EventArgs e) { featureList.Enabled = true; featureList.Items.Clear(); string dbConn = @"server=localhost\OfficeServers;initial catalog=SharePoint_Config_66140120-a9bf-4191-86b6-ec21810ca019;IntegratedSecurity=SSPI;"; _rootCollection = new SPSite(siteUrl.Text); SPFarm farm = SPFarm.Open(dbConn); statusLabel.Text = "Site Feature Status (" + farm.FeatureDefinitions.Count.ToString() + " Feature Definitions Installed)"; foreach (SPFeatureDefinition featureDefinition in farm.FeatureDefinitions) { ListViewItem lvi = new ListViewItem(featureDefinition.DisplayName); if (featureDefinition.Hidden) lvi.ForeColor = Color.Gray; lvi.Tag = featureDefinition.Id; lvi.SubItems.Add(GetFeatureEnabled(featureDefinition)); featureList.Items.Add(lvi); } } } }