删除内容类型

上次修改时间: 2010年11月1日

适用范围: SharePoint Foundation 2010

在 SharePoint Online 中提供

如果网站内容类型是作为其他网站的基础或列表内容类型使用,则无法删除此网站内容类型。必须先从所有使用此内容类型的列表中移除它,并删除所有基于该类型的子网站内容类型。

如果列表包含某内容类型的项目,则无法从该列表中删除此内容类型。不过,Microsoft SharePoint Foundation 不会考虑回收站中的项目。如果在从列表中删除这些项目的内容类型之后恢复它们,则这些项目将分配给该列表的默认内容类型。

使用对象模型删除内容类型

要从列表或文档库中的内容类型集合中删除内容类型,应先访问 SPList 对象(服务器)或 List 对象(客户端)的 ContentTypes 属性中的集合。然后调用 Delete 方法,以传递标识要删除的内容类型的 SPContentTypeId(服务器)结构或 ContentTypeId(客户端)结构。

要从网站集中删除内容类型,请访问 SPWeb 对象(服务器)或 Web 对象(客户端)的 ContentTypes 属性中的集合。然后调用 Delete 方法。

警告注释警告

SPWeb 对象(服务器)和 Web 对象(客户端)都具有一个返回内容类型集合的 AvailableContentTypes 属性。该集合是只读的。您不能从中删除对象。这是因为该集合包含当前网站中提供 的所有内容类型,而不仅仅是在当前网站中定义 的内容类型。

在这两种情形下,您都必须记住,不能删除正在使用的内容类型。如果尝试从列表中删除内容类型,则必须先确保没有列表项使用该内容类型。为此,可循环访问列表中的项目,然后查找每个项目的 ContentType 属性的值。如果要尝试从定义内容类型的网站集中删除该内容类型,则必须确保 GetUsages 方法返回空列表,即,该内容类型未在任何列表上使用,并且不是任何子内容类型的父级。

示例

下面的控制台应用程序示例验证当前网站或任意子网站中是否正在使用已过时的内容类型。如果未使用该内容类型,则应用程序会将其删除。

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"];

               // We have a content type.
               if (obsolete != null) 
               {
                  IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(obsolete);

                  // It is in use.
                  if (usages.Count > 0) 
                  {
                     Console.WriteLine("The content type is in use in the following locations:");
                     foreach (SPContentTypeUsage usage in usages)
                        Console.WriteLine(usage.Url);
                  }

                  // The content type is not in use.
                  else 
                  {

                     // Delete it.
                     Console.WriteLine("Deleting content type {0}...", obsolete.Name);
                     webSite.ContentTypes.Delete(obsolete.Id);
                  }
               }

               // No content type found.
               else 
               {
                  Console.WriteLine("The content type does not exist in this site collection.");
               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}
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")

            ' We have a content type
            If obsolete IsNot Nothing Then 
               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

               ' It is not in use.
               Else 

                  ' Delete it.
                  Console.WriteLine("Deleting content type {0}...", obsolete.Name)
                  webSite.ContentTypes.Delete(obsolete.Id)
               End If

            ' No content type found.
            Else 
               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

请参阅

引用

SPContentTypeUsage

概念

内容类型简介

创建内容类型

更新子内容类型