Click to Rate and Give Feedback
Community Content
In this section
Statistics Annotations (1)
Collapse All/Expand All Collapse All
This page is specific to
The 2007 product release

Other versions are also available for the following:
SPFeatureCollection Class (Microsoft.SharePoint)
Represents a collection of SPFeature objects.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
<DefaultMemberAttribute("Item")> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
Public NotInheritable Class SPFeatureCollection
    Implements ICollection
Visual Basic (Usage)
Dim instance As SPFeatureCollection
C#
[DefaultMemberAttribute("Item")] 
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
public sealed class SPFeatureCollection : ICollection

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 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

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.

C#
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();
            }
    }
}
NoteNote:

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 Best Practices: Using Disposable Windows SharePoint Services Objects.

System.Object
  Microsoft.SharePoint.SPFeatureCollection
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Requires directive      Vijay Gande ... VijayGande   |   Edit   |   Show History

It also requires the following directive for SPFarm, PFeatureDefinitionCollection..etc objects


using Microsoft.SharePoint.Administration;

if the feature already exists in one of the web then the above listed code will throws an exception and comes out of loop and never adds it to the other sites, the followign code checks if feature exists in web if not then adds(Activates) it.



public static void ActiveDocLibEvntHandlerInAllWebs(string siteCollName, String featureDisplayName)
{
logWriter.WriteLine(DateTime.Now.ToString() + "-- Entering ActiveDocLibEvntHandlerInAllWebs()");
System.Globalization.CultureInfo oCultureInfo = new System.Globalization.CultureInfo(1033);
SPFeatureDefinitionCollection collFeatureDefinitions = SPFarm.Local.FeatureDefinitions;
foreach (SPFeatureDefinition oFeatureDefinition in collFeatureDefinitions)
{
if (oFeatureDefinition.GetTitle(oCultureInfo) == featureDisplayName)
{
int count = 0;
Guid guidFeatureDefinitionID = oFeatureDefinition.Id;
using (SPSite site = new SPSite(siteCollName))
{
foreach (SPWeb oWebsite in site.AllWebs)
{
count++;
if (oFeatureDefinition.Scope == SPFeatureScope.Web)
{
SPFeatureCollection collFeatureCollection = oWebsite.Features;
try
{
SPFeature feature = oWebsite.Features[guidFeatureDefinitionID];
logWriter.WriteLine(DateTime.Now.ToString() + " -- Iteration# " + count.ToString() + " -- " + SPEncode.HtmlEncode(feature.Definition.GetTitle(oCultureInfo)) + " feature already exists on " + oWebsite.Title);
}
catch (Exception ex)
{
SPFeature oFeature = collFeatureCollection.Add(guidFeatureDefinitionID);
logWriter.WriteLine(DateTime.Now.ToString() + " -- Iteration# " + count.ToString() + " -- " + SPEncode.HtmlEncode(oFeature.Definition.GetTitle(oCultureInfo)) + " feature added on " + oWebsite.Title);
}
}
oWebsite.Dispose();
}
}
}
}
logWriter.WriteLine(DateTime.Now.ToString() + "-- Closing ActiveDocLibEvntHandlerInAllWebs()");
}

Regards,
Vijay Gande

Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker