Compartilhar via


Deleting Content Types

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

You cannot delete a site content type if it is being used as the basis for other site or list content types. You must first remove this content type from all lists that use it and delete all child site content types that are based on it.

You cannot delete a content type from a list if that list contains items of that content type. However, Microsoft SharePoint Foundation does not consider items that are in the Recycle Bin. If those items are restored after their content type is deleted from the list, those items are assigned the default content type for that list.

Using the Object Model to Delete a Content Type

To remove a content type from the collection of content types on a list or document library, you first access the collection in the ContentTypes property of the SPList object (server) or the List object (client). Then call the Delete method, passing an SPContentTypeId (server) structure or the ContentTypeId (client) structure that identifies the content type that you want to delete.

To remove a content type from a site collection, access the collection in the ContentTypes property of the SPWeb object (server) or the Web object (client). Then call the Delete method.

Warning

Both the SPWeb object (server) and Web object (client) have an AvailableContentTypes property that returns a content type collection. This collection is read-only. You cannot delete objects from it. That is because the collection contains all content types that are available in the current site, not only those that are defined in the current site.

In both cases, you must be aware that you cannot delete a content type that is being used. If you are trying to remove a content type from a list, you must first ensure that no list items use the content type. One way to do that would be to iterate through the items in the list and look at the value of the ContentType property of each item. If you are trying to remove a content type from the site collection where it is defined, you must be sure that the GetUsages method returns an empty list—that is, that the content type is not used on any list and is not the parent of any child content type.

Example

The following example shows a console application that verifies whether an obsolete content type is being used in the current website or any child sites. If the content type is not being used, the application deletes it.

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

See Also

Reference

SPContentTypeUsage

Concepts

Introduction to Content Types

Creating Content Types

Updating Child Content Types