共用方式為


SPContentTypeCollection.Delete method

從集合中刪除指定的內容類型。

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'宣告
Public Sub Delete ( _
    id As SPContentTypeId _
)
'用途
Dim instance As SPContentTypeCollection
Dim id As SPContentTypeId

instance.Delete(id)
public void Delete(
    SPContentTypeId id
)

參數

Exceptions

Exception Condition
ArgumentOutOfRangeException

集合中的沒有內容型別具有指定的內容類型識別碼。

SPException

內容的型別是在清單上的最後一個內容類型。無法刪除清單上的最後一個內容類型。

SPException

內容的型別是在清單上的最後一個內容類型。無法刪除清單上的最後一個內容類型。

內容類型是網站或清單的內容類型的父系。您不能刪除正在使用中的內容類型。

-或-

內容型別屬於使用中的功能。

-或-

內容型別集合是唯讀的。

SPContentTypeSealedException

指定的內容型別為密封。

SPContentTypeReadOnlyException

指定的內容類型是唯讀的。

備註

如果它正在使用為基礎的其他網站或清單的內容類型,您無法刪除網站的內容類型。您首先必須使用它的所有清單中移除這個內容型別,並刪除所有子網站的內容類型為基礎的。

如果該清單包含該內容類型的項目,您無法從清單刪除內容的型別。SharePoint Foundation不會考慮進行這項決定時,傳送至資源回收筒] 的項目。如果從清單中刪除其內容類型之後,會還原那些項目,這些項目會指定該清單的預設內容類型。

當指定要刪除的內容類型 ID,請記住的網站與清單內容的型別 Id 衍生自的內建的 Id 內容型別,但它們不是內建的內容型別 Id 相同。例如,下列程式碼會嘗試刪除 Item 內容類型藉由指定的內容型別 ID 為SPBuiltInContentTypeId.Item :

list.ContentTypes.Delete(SPBuiltInContentTypeId.Item); // Throws an exception.

因為清單的內容型別集合不包含SPBuiltInContentTypeId.Item的內容 id 型別程式碼,就會擲回例外狀況ArgumentOutOfRangeException 。它,不過,包含內容的型別,具有 ID 衍生自,因此是關閉的符合項目,內建的內容型別 id。下列程式碼說明正確的方式來尋找並刪除的項目內容類型的清單的複本。

SPContentTypeId id = list.ContentTypes.BestMatch(SPBuiltInContentTypeId.Item);
list.ContentTypes.Delete(id);

Examples

下列範例會驗證是否已過時的內容類型為目前的 Web 站台或任何子網站中的使用中的主控台應用程式。如果內容類型不在使用中,應用程式會刪除它。

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 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 found.
               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 // The content type is not in use.
                  {
                     // Delete it.
                     Console.WriteLine("Deleting content type {0}...", obsolete.Name);
                     webSite.ContentTypes.Delete(obsolete.Id);
                  }
               }
               else // No content type found.
               {
                  Console.WriteLine("The content type does not exist in this site collection.");
               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}

請參閱

參照

SPContentTypeCollection class

SPContentTypeCollection members

Microsoft.SharePoint namespace

SPContentTypeUsage

其他資源

Content Type IDs

Introduction to Content Types

Site and List Content Types

Base Content Type Hierarchy