SPFeatureCollection Class
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
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.
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(); } } }
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 Best Practices: Using Disposable Windows SharePoint Services Objects. |
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
- 8/7/2009
- Vijay Gande
- 10/2/2009
- VijayGande
Note: