共用方式為


SPFeatureCollection.Add Method (Guid, Boolean)

Activates a Feature by using the Feature definition with the specified ID 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 _
) As SPFeature
'Usage
Dim instance As SPFeatureCollection
Dim featureId As Guid
Dim force As Boolean
Dim returnValue As SPFeature

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

Parameters

  • force
    Type: System.Boolean

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

Return Value

Type: Microsoft.SharePoint.SPFeature
An object that represents the feature instance. The FeatureDefinitionScope property of the SPFeature object is set to SPFeatureDefinitionScope.Farm.

Exceptions

Exception Condition
InvalidOperationException

The Feature definition has not been installed in the server farm, or 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 deployed at the farm level or at the site-collection level. This overload of the Add method can be used only with Feature definitions that have been deployed at the farm level. To activate a Feature using a Feature definition that is deployed at the site collection level—that is, a Feature definition made available through sandboxed solutions—use the Add(Guid, Boolean, SPFeatureDefinitionScope) overload of the Add method.

You can get the collection of SPFeatureDefinition objects that represent Feature definitions installed at the farm level by accessing the FeatureDefinitions property of the SPFarm object. A feature definition that is in the farm-level collection can be activated for a Web application, a site collection, or a Web site—as long as the Scope property of the SPFeatureDefinition object matches the level of the collection to which you are adding the Feature.

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