SPFeatureCollection.Add Method (Guid)
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.
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 _
) As SPFeature
'Usage
Dim instance As SPFeatureCollection
Dim featureId As Guid
Dim returnValue As SPFeature
returnValue = instance.Add(featureId)
public SPFeature Add(
Guid featureId
)
Parameters
featureId
Type: System.GuidThe value of the Id property of an SPFeatureDefinition object.
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.