Sunday, April 28, 2013

"The Content Type is in Use" Error in SharePoint - Find Where and Delete

We don't needed a particular content type anymore and wanted to delete the content type.

Navigated to:
  •     Site Actions >> Site Settings.
  •     Under Galleries click Site content types.
  •     Select the Content type to delete by clicking on its name.
  •     Click "Delete this site content type" link.
Oops!  SharePoint returns an error: Error: "The Content Type is in Use".  
the content type is in use sharepoint
Looks like its a common issue when trying to delete content types. I've seen this "The content type is in use" error in SharePoint 2007 and in SharePoint 2010. The error message clearly indicates that the content type is still in use and cannot be deleted. Yes, We had assigned the content type to some list and libraries. But I dunno where!

How to delete a content type and get rid of "The content type is in use" error in SharePoint? Well, to solve this issue: We've to Find the locations where the particular content type is being used and delete the the associations and items.

Find the locations where our Content type is in use:

Lets use "SharePoint Manager 2010" to find where our content type is being used.
  • Download the SharePoint Manager 2010 from Codeplex
  • Open SPM and Navigate to the target Site Collection where your Content Type is located
  • Go to Content Type >> Usages to find all content type usages
  • Delete all the usages of the Content Typecannot delete content type the content type is in use
Once done, You can delete your content type with out any issues. We can use PowerShell also.

 

PowerShell Script to find where a specific Content Type is being used:

We can use PowerShell also to find where a specific Content Type is being used:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the site
$site = Get-SPSite "http://sharepoint.crescent.com/sites/marketing"
#Name of the Content Type
$ContentTypeName = "Travel Request"
  
#Loop through all web
 Foreach ($web in $site.AllWebs)
    {
  #Get the Content Type
  $ContentType = $web.ContentTypes[$ContentTypeName]
  
  #Check if content type exists
     if($ContentType -ne $null)
  {
   #Get the Content Type Usage
   $CTypeUsages = [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($ContentType)

   if ($CTypeUsages.Count -gt 0) 
   {
       foreach ($CTypeUsage in $CTypeUsages) 
    {
            write-host $CTypeUsage.Url
       }
   }
  }
  $web.Dispose();
 }
This will provide list of locations where the specific content type is being utilized. Now, We've to Delete those items or change their content types and remove the content type associations from lists and libraries.

Change Content Type Programmatically:

If you want to delete a content type, You have to make sure No Lists/Library and No List Item/Document is using the content type! In other words, You got to either change the content type of existing items created with the particular content type or delete those items using the specific content type!!

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the Web
$site = Get-SPSite "http://sharepoint.crescent.com/sites/marketing"
#Name of the Content Type
$ContentTypeName = "Travel Request"
$NewCTypeName="Travel Request V2"

#Loop through all web
 Foreach ($web in $site.AllWebs)
    {
  #Get the Content Type
  $ContentType = $web.ContentTypes[$ContentTypeName]
  
  #Check if content type exists
     if($ContentType -ne $null)
  {
   #Get the Content Type Usage
   $CTypeUsages = [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($ContentType)

   if ($CTypeUsages.Count -gt 0) 
   {
       foreach ($CTypeUsage in $CTypeUsages) 
    {
           #Get the list where specific content type in use write-host 
     $list = $web.GetList($CTypeUsage.Url)
     #Check for Items using the content type
     $SPQuery = "<Where><Eq><FieldRef Name='ContentType'/><Value Type='Text'>$($ContentTypeName)</Value></Eq></Where>"
           $ListItems = $list.GetItems($SPQuery)
     #Change the Content Type of All existing Items which are currently using the specific content type
           for($i=$ListItems.Count-1; $i -ge 0; $i--)
           {
      $ListItem= $ListItems[$i]
      write-host "Changing Content Type of List Item with ID: $($ListItem.ID)"
               $ListItem["ContentTypeId"]= $list.ContentTypes[$NewCTypeName].id
      $ListItem.Update()
           }
  
     #Delete the content type from list
     $list.ContentTypes.Delete($list.ContentTypes[$ContentTypeName].id)
           write-host "Deleted the Content type from:$($CTypeUsage.Url)" 
     
    }
   }
  #Delete the Content Type now!
  $ContentType.Delete()
  write-host "Content Type $($ContentTypeName) Deleted!"
  }
  $web.Dispose();
 }

 

Deleting a Particular Content Type including all items based on the content type:

Delete All Items which are currently using the specific content type
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the Web
$site = Get-SPSite "http://sharepoint.crescent.com/sites/marketing"
#Name of the Content Type
$ContentTypeName = "Travel Request"
  
#Loop through all web
 Foreach ($web in $site.AllWebs)
    {
  #Get the Content Type
  $ContentType = $web.ContentTypes[$ContentTypeName]
  
     #Check if content type exists
     if($ContentType -ne $null)
  {
   #Get the Content Type Usage
   $CTypeUsages = [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($ContentType)

   if ($CTypeUsages.Count -gt 0) 
   {
       foreach ($CTypeUsage in $CTypeUsages) 
    {
           #Get the list where specific content type in use
     $list = $web.GetList($CTypeUsage.Url)
     #Check for Items using the content type
     $SPQuery = "<Where><Eq><FieldRef Name='ContentType'/><Value Type='Text'>$($ContentTypeName)</Value></Eq></Where>"
           $ListItems = $list.GetItems($SPQuery)
           for($i=$ListItems.Count-1; $i -ge 0; $i--)
           {
                   write-host "Deleted List Item with ID: $($ListItems[$i].ID)"
                   $DeletedItem= $ListItems[$i].recycle()
           }
  
     #Delete the content type from list
     $list.ContentTypes.Delete($list.ContentTypes[$ContentTypeName].id)
                   write-host "Deleted the Content type from:$($CTypeUsage.Url)" 
     
    }
   }
  #Delete the Content Type now!
  $ContentType.Delete()
  write-host "Content Type $($ContentTypeName) Deleted!"
  }
  $web.Dispose();
 }

BTW, I read somewhere, To fix "the content type is in use" error in SharePoint 2010, we must delete the list items of particular content type from both End-user Recycle bin & Admin Recycle bin! But I didn't delete them from recycle bin and still I'm able to delete the content types. I didn't face any issue so far.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...