共用方式為


SPFeatureCollection.Add Method (Guid, Boolean, SPFeatureDefinitionScope)

Activates a feature by using the feature definition with the specified ID at the specified scope to create a new SPFeature object and then add it to the collection, optionally forcing activation even if there are errors.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Function Add ( _
    featureId As Guid, _
    force As Boolean, _
    featdefScope As SPFeatureDefinitionScope _
) As SPFeature
'Usage
Dim instance As SPFeatureCollection
Dim featureId As Guid
Dim force As Boolean
Dim featdefScope As SPFeatureDefinitionScope
Dim returnValue As SPFeature

returnValue = instance.Add(featureId, _
    force, featdefScope)
public SPFeature Add(
    Guid featureId,
    bool force,
    SPFeatureDefinitionScope featdefScope
)

Parameters

  • force
    Type: System.Boolean

    true to force activation of the Feature even if there are errors; otherwise, false.

  • featdefScope
    Type: Microsoft.SharePoint.SPFeatureDefinitionScope

    An enumeration value that indicates where the Feature definition is deployed. To activate an instance of a Feature that is made available through sandboxed solutions, pass SPFeatureDefinitionScope.Site as the argument. Otherwise, pass SPFeatureDefinitionScope.Farm.

Return Value

Type: Microsoft.SharePoint.SPFeature
An object that represents the Feature instance. The FeatureDefinitionScope property of the SPFeature object is set to the value that is passed in the featdefScope parameter.

Exceptions

Exception Condition
InvalidOperationException

The value of the Scope property of the SPFeatureDefinition object does not match the level of the collection to which you are adding the feature.

SPFeatureDependencyNotActivatedException

The Feature is dependent on another Feature that has not been activated at this scope.

Remarks

Feature definitions can be installed at either the farm level or the site-collection level. Use the FeatureDefinitions property of the SPFarm object to access the collection of SPFeatureDefinition objects that represent feature definitions installed at the farm level. Use the FeatureDefinitions property of the SPSite object to access the collection installed at the site-collection level.

A feature definition that is installed at the farm level can be activated for a Web application, a site collection, or a Web site. In all three cases, you must pass SPFeatureDefinitionScope.Farm in the featdefScope of the Add method. This parameter simply tells the Add method where to look for the feature definition.

A Feature definition that is installed at the site-collection level can be activated for a site collection or a Web site. In either case, you must pass SPFeatureDefinitionScope.Site in the featdefScope of the Add method.

To activate a Feature definition for a Web application, add it to the SPFeatureCollection object that is returned by the Features property of an SPWebApplication object. To activate it for a site collection, add it to the collection that is returned by the Features property of an SPSite object. To activate it for a Web site, add it to the collection that is returned by the Features property of an SPWeb object. The Add method throws an InvalidOperationException exception if the value of the Scope property of the SPFeatureDefinition object does not match the level of the collection to which you are adding the Feature.

See Also

Reference

SPFeatureCollection Class

SPFeatureCollection Members

Add Overload

Microsoft.SharePoint Namespace