SPFeatureCollection class
表示SPFeature物件的集合。
Inheritance hierarchy
System.Object
Microsoft.SharePoint.SPFeatureCollection
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public NotInheritable Class SPFeatureCollection _
Implements ICollection, IEnumerable(Of SPFeature), _
IEnumerable
'用途
Dim instance As SPFeatureCollection
public sealed class SPFeatureCollection : ICollection,
IEnumerable<SPFeature>, IEnumerable
備註
使用Microsoft.SharePoint.Administration.SPWebApplication、 Microsoft.SharePoint.Administration.SPWebService、 SPSite或SPWeb類別的Features屬性來取得啟動 Web 應用程式、 Web 服務、 網站集合或網站中的功能集合。使用SPContext類別的SiteFeatures或WebFeatures屬性來取得目前的網站集合或網站的啟動功能的集合。
這些集合的其中一Feature物件的存在指出指定的範圍內將它啟動。若要啟動的功能,您必須安裝在伺服器陣列中 ;若要安裝的功能,請使用SPFeatureCollection類別的Add方法。
若要從集合傳回單一的Feature物件使用索引子。例如,如果集合指派給一個名為collFeatures變數中,使用在 C# 的collFeatures[index]或 Visual Basic, index的Feature物件的 GUID 的位置中的collFeatures(index) 。
Examples
下列程式碼範例會啟動指定的標題中的特定網站集合的所有子網站的 Web 站台範圍功能。
這個範例要求using指示詞 (在 Visual Basic 的Imports ) 的Microsoft.SharePoint和Microsoft.SharePoint.Utilities命名空間。
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
注意事項 |
---|
某些物件實作IDisposable介面,並且您必須避免之後不再需要保留這些物件在記憶體中。良好的程式碼撰寫方式的相關資訊,請參閱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.