SPContentTypeUsage class
用來追蹤其內容類型用為基礎來另一個內容類型。
Inheritance hierarchy
System.Object
Microsoft.SharePoint.SPContentTypeUsage
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public NotInheritable Class SPContentTypeUsage
'用途
Dim instance As SPContentTypeUsage
public sealed class SPContentTypeUsage
備註
SPContentTypeUsage物件包含其中內容的類型用於作為另一個內容類型的單一執行個體的相關資訊。例如,當站台的內容類型套用至清單時,子系的站台內容類型建立並加入至清單的內容型別集合。新清單的內容型別是執行個體,父網站的內容類型 「 使用。SPContentTypeUsage物件可以提供這種使用方式,例如統一資源定位器 (URL) 的根資料夾清單中有衍生的內容類型的相關資訊。
注意事項 |
---|
內容類型是 「 使用 」 從它衍生的任何內容類型是否有在網站或清單層級範圍內的任何地方SPContentTypeCollection集合的父系內容類型中。 |
若要取得整個網站集合的內容類型的所有使用的清單,請呼叫靜態GetUsages(SPContentType)方法,它會傳回SPContentTypeUsage物件的一般清單。
Examples
下列範例會驗證是否已過時的內容類型為目前的站台或任何子網站中的使用中的主控台應用程式。如果內容類型不在使用中,應用程式會刪除它。
Imports System
Imports System.Collections.Generic
Imports Microsoft.SharePoint
Module ConsoleApp
Sub Main()
Using siteCollection As SPSite = New SPSite("https://localhost")
Using webSite As SPWeb = siteCollection.OpenWeb()
' Get the obsolete content type.
Dim obsolete As SPContentType = webSite.ContentTypes("Test")
If obsolete IsNot Nothing Then ' We have a content type
Dim usages As IList(Of SPContentTypeUsage) = _
SPContentTypeUsage.GetUsages(obsolete)
If usages.Count > 0 Then ' It is in use
Console.WriteLine("The content type is in use in the following locations:")
For Each usage As SPContentTypeUsage In usages
Console.WriteLine(usage.Url)
Next usage
Else ' It is not in use.
' Delete it.
Console.WriteLine("Deleting content type {0}...", obsolete.Name)
webSite.ContentTypes.Delete(obsolete.Id)
End If
Else ' No content type available.
Console.WriteLine("The content type does not exist in this site collection.")
End If
End Using
End Using
Console.Write(vbCrLf + "Press ENTER to continue...")
Console.ReadLine()
End Sub
End Module
using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
namespace Test
{
class ConsoleApp
{
static void Main(string[] args)
{
using (SPSite siteCollection = new SPSite("https://localhost"))
{
using (SPWeb webSite = siteCollection.OpenWeb())
{
// Get the obsolete content type.
SPContentType obsolete = webSite.ContentTypes["Test"];
if (obsolete != null) // We have a content type
{
IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(obsolete);
if (usages.Count > 0) // It is in use
{
Console.WriteLine("The content type is in use in the following locations:");
foreach (SPContentTypeUsage usage in usages)
Console.WriteLine(usage.Url);
}
else // It is not in use.
{
// Delete it.
Console.WriteLine("Deleting content type {0}...", obsolete.Name);
webSite.ContentTypes.Delete(obsolete.Id);
}
}
else // No content type is found.
{
Console.WriteLine("The content type does not exist in this site collection.");
}
}
}
Console.Write("\nPress ENTER to continue...");
Console.ReadLine();
}
}
}
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.
請參閱
參照
Microsoft.SharePoint namespace