SPContentTypeUsage 类

用于跟踪在内容类型用作基础的另一个内容类型。

继承层次结构

System.Object
  Microsoft.SharePoint.SPContentTypeUsage

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public NotInheritable Class SPContentTypeUsage
用法
Dim instance As SPContentTypeUsage
public sealed class SPContentTypeUsage

备注

SPContentTypeUsage对象包含在内容类型用作基础的另一个内容类型的单个实例的相关信息。例如,当网站内容类型应用于列表中,网站内容类型的子级创建并将其添加到列表的内容类型集合。新的列表内容类型是使用父网站内容类型"。"SPContentTypeUsage对象可以提供有关使用,如统一资源定位器 (URL) 的列表,其中包含派生的内容类型的根文件夹的信息。

备注

内容类型"使用"其派生的任何内容类型是否存在父内容类型在网站或列表范围内的任意位置级别SPContentTypeCollection集合中。

要获取整个网站集的内容类型的所有用途的列表,请调用静态GetUsages(SPContentType)方法,它返回SPContentTypeUsage对象的泛型列表。

示例

下面的示例演示一个验证已过时的内容类型是否为当前网站或任何子网站中使用的控制台应用程序。如果内容类型不是使用中,应用程序将其删除。

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();
      }
   }
}

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

SPContentTypeUsage 成员

Microsoft.SharePoint 命名空间

SPFieldTemplateUsage

其他资源

Site and List Content Types

Creating Content Types Based on Other Content Types

Content Type Scope