コンテンツ タイプを削除する

最終更新日: 2010年11月1日

適用対象: SharePoint Foundation 2010

SharePoint Online で使用可能

あるサイト コンテンツ タイプが、他のサイト コンテンツ タイプまたはリスト コンテンツ タイプの基礎として使用されている場合、そのサイト コンテンツ タイプを削除することはできません。サイト コンテンツ タイプを削除するには、まず、使用されているすべてのリストからそのサイト コンテンツ タイプを削除し、そのサイト コンテンツ タイプに基づいているすべての子サイト コンテンツ タイプを削除する必要があります。

あるコンテンツ タイプの項目がリストに格納されている場合、そのコンテンツ タイプをリストから削除することはできません。ただし、Microsoft SharePoint Foundation はごみ箱の項目を考慮しません。リストからコンテンツ タイプが削除された後に、そのコンテンツ タイプの項目が復元された場合、これらの項目にはリストの既定のコンテンツ タイプが割り当てられます。

オブジェクト モデルを使用してコンテンツ タイプを削除する

リストまたはドキュメント ライブラリのコンテンツ タイプのコレクションからコンテンツ タイプを削除するには、まず、SPList オブジェクト (サーバー) または List オブジェクト (クライアント) の ContentTypes プロパティのコレクションにアクセスします。次に、Delete メソッドを呼び出して、削除するコンテンツ タイプを識別する SPContentTypeId (サーバー) 構造体または ContentTypeId (クライアント) 構造体を渡します。

サイト コレクションからコンテンツ タイプを削除するには、SPWeb オブジェクト (サーバー) または Web オブジェクト (クライアント) の ContentTypes プロパティのコレクションにアクセスし、Delete メソッドを呼び出します。

注意注意

SPWeb オブジェクト (サーバー) と Web オブジェクト (クライアント) の両方に、コンテンツ タイプ コレクションを返す AvailableContentTypes プロパティがあります。このコレクションは読み取り専用なので、そこからオブジェクトを削除することはできません。これは、コレクションには、現在のサイトで定義されているコンテンツ タイプだけでなく、現在のサイトで使用できるすべてのコンテンツ タイプが含まれるからです。

どちらの場合も、使用中のコンテンツ タイプは削除できないことに注意してください。リストからコンテンツ タイプを削除する場合は、まず、そのコンテンツ タイプを使用しているリスト アイテムがないことを確認しておく必要があります。これを行う 1 つの手段として、リスト内のアイテムに対して反復処理を行い、各アイテムの ContentType プロパティの値を確認します。コンテンツ タイプが定義されているサイト コレクションから、そのコンテンツ タイプを削除するには、GetUsages メソッドによって空のリストが返されること、つまり、そのコンテンツ タイプを使用しているリストがないこと、およびそのコンテンツ タイプが親になっている子コンテンツ タイプがないことを確認する必要があります。

以下の例は、古いコンテンツ タイプが現在の Web サイトまたは任意の子サイトで使用されているかどうかを確認するコンソール アプリケーションを示しています。使用されていない場合、そのコンテンツ タイプはアプリケーションによって削除されます。

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

概念

コンテンツ タイプについて

コンテンツ タイプの作成

子コンテンツ タイプを更新する