Unable to delete SharePoint 2010 ContentType "Contenty type in use."

Viewed 46831

I have tried all the recommendations on the web, to no avail.

I wrote a console application per these instructions: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontenttypecollection.delete.aspx

The "Usages.Count" is = 0. Yet, when it tries to delete the Content Type I get an Exception:

"The content type is in use."

This is a brand new (development) install. I created a test site in SP Designer, created a Content Type,then a list. Then, I removed the list, removed it from Recycle Bin and tried to remove the content type...... Ugh.

4 Answers

this powershell script form this post also worked for me

$siteURL = "The Site url"
$contentType = "Content type Name"

$web = Get-SPWeb $siteURL
$ct = $web.ContentTypes[$contentType]

if ($ct) {
$ctusage = [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($ct)
      foreach ($ctuse in $ctusage) {
        $list = $web.GetList($ctuse.Url)
        $contentTypeCollection = $list.ContentTypes;
        $contentTypeCollection.Delete($contentTypeCollection[$contentType].Id);
        Write-host "Deleted $contentType content type from $ctuse.Url"
        }
$ct.Delete()
Write-host "Deleted $contentType from site."

} else { Write-host "Nothing to delete." }

$web.Dispose()
  

    using System;
    using System.Collections.Generic;
    using Microsoft.SharePoint;

    namespace Test
    {
       class ConsoleApp
       {
          static void Main(string[] args)
          {
             using (SPSite siteCollection = new SPSite("http://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 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();
          }
       }
    }

 

Create a Console Application with the above code and run that project. This code will tell you the libraries in which the content types are attached. Then simply go that libraries and delete the attached content types. Then finally delete the content type from Site Actions -> Site Settings -> Site Content Types or you may use the above code as well to delete the content type.

This worked for me hope it may also work for you !!! Thanks.

Related