Partilhar via


SPFeatureCollection Class

Represents a collection of SPFeature objects.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.SPFeatureCollection

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

Syntax

'Declaration
<ClientCallableTypeAttribute(Name := "FeatureCollection", CollectionChildItemType := GetType(SPFeature),  _
    ServerTypeId := "{8b9c0015-193d-4062-8e98-8d23c303eedd}")> _
<SubsetCallableTypeAttribute> _
<SubsetCallableExcludeInterfaceAttribute(SubsetCallableExcludeMemberType.ExplicitInterfaceProperty, ExcludeInterface := GetType(ICollection))> _
Public NotInheritable Class SPFeatureCollection _
    Implements ICollection, IEnumerable(Of SPFeature),  _
    IEnumerable
'Usage
Dim instance As SPFeatureCollection
[ClientCallableTypeAttribute(Name = "FeatureCollection", CollectionChildItemType = typeof(SPFeature), 
    ServerTypeId = "{8b9c0015-193d-4062-8e98-8d23c303eedd}")]
[SubsetCallableTypeAttribute]
[SubsetCallableExcludeInterfaceAttribute(SubsetCallableExcludeMemberType.ExplicitInterfaceProperty, ExcludeInterface = typeof(ICollection))]
public sealed class SPFeatureCollection : ICollection, 
    IEnumerable<SPFeature>, IEnumerable

Remarks

Use the Features property of the Microsoft.SharePoint.Administration.SPWebApplication, Microsoft.SharePoint.Administration.SPWebService, SPSite, or SPWeb class to get the collection of Features that are activated in the Web application, Web service, site collection, or site. Use the SiteFeatures or WebFeatures property of the SPContext class to get the collection of activated Features for the current site collection or site.

The existence of a Feature object within one of these collections indicates that it has been activated within the given scope. To activate a Feature, you must install it in the server farm; to install a feature, use an Add method of the SPFeatureCollection class.

Use an indexer to return a single Feature object from the collection. For example, if the collection is assigned to a variable named collFeatures, use collFeatures[index] in C#, or collFeatures(index) in Visual Basic, where index is the GUID of the Feature object.

Examples

The following code example activates a Web site-scoped Feature with the specified title in all the subsites of a specific site collection.

This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

System.Globalization.CultureInfo oCultureInfo = new 
    System.Globalization.CultureInfo(1033);
SPFeatureDefinitionCollection collFeatureDefinitions = 
    SPFarm.Local.FeatureDefinitions;

foreach (SPFeatureDefinition oFeatureDefinition in 
    collFeatureDefinitions)
{
    if (oFeatureDefinition.GetTitle(oCultureInfo) == "Feature_Title")
    {
        Guid guidFeatureDefinitionID = oFeatureDefinition.Id;
        SPWebCollection collWebsites = 
            SPContext.Current.Site.AllWebs["Site"].Webs;
            foreach (SPWeb oWebsite in collWebsites)
            {
                 if (oFeatureDefinition.Scope == SPFeatureScope.Web)
                 {
                     SPFeatureCollection collFeatureCollection = 
                         oWebsite.Features;
                     SPFeature oFeature = 
collFeatureCollection.Add(guidFeatureDefinitionID);
                        Response.Write(SPEncode.HtmlEncode(oFeature.Definition.GetTitle(oCultureInfo)) + " feature added on " + oWebsite.Title + "<BR>");
                }
                oWebsite.Dispose();
            }
    }
}
Dim oCultureInfo As New System.Globalization.CultureInfo(1033)
Dim collFeatureDefinitions As SPFeatureDefinitionCollection = SPFarm.Local.FeatureDefinitions

For Each oFeatureDefinition As SPFeatureDefinition In collFeatureDefinitions
    If oFeatureDefinition.GetTitle(oCultureInfo) = "Feature_Title" Then
        Dim guidFeatureDefinitionID As Guid = oFeatureDefinition.Id
        Dim collWebsites As SPWebCollection = SPContext.Current.Site.AllWebs("Site").Webs
            For Each oWebsite As SPWeb In collWebsites
                 If oFeatureDefinition.Scope = SPFeatureScope.Web Then
                     Dim collFeatureCollection As SPFeatureCollection = oWebsite.Features
                     Dim oFeature As SPFeature = collFeatureCollection.Add(guidFeatureDefinitionID)
                        Response.Write(SPEncode.HtmlEncode(oFeature.Definition.GetTitle(oCultureInfo)) & " feature added on " & oWebsite.Title & "<BR>")
                 End If
                oWebsite.Dispose()
            Next oWebsite
    End If
Next oFeatureDefinition

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SPFeatureCollection Members

Microsoft.SharePoint Namespace