Thursday, May 30, 2013

Locate Where a Content Type is Being Used on Your Site

I always run into the problem of wanting to delete an old Content Type but receive the error from SharePoint saying that it is "still in use". Instead of wasting time checking where the Content Type is in use for each list or library, just use the PowerShell script below as was posted by "Dave the SharePoint Engineer":

$targetCT = "ENTER CONTENT TYPE NAME HERE"
$targetWebApp = "ENTER YOUR SITE URL HERE"
$webapp = Get-SPWebApplication $targetWebApp
$sites = $webapp.Sites
foreach ($site in $sites)
{
  foreach ($web in $site.AllWebs)
  {
    foreach ($ct in $web.ContentTypes)
    {
       if($ct.Name -eq $targetCT)
       {
          Write-Host $targetCT is defined as a content type in $web.Url
       }
    }
    foreach ($list in $web.Lists)
    {
       foreach ($listct in $list.ContentTypes)
       {
         if($listct.Name -eq $targetCT)
         {
            Write-Host $targetCT is defined in list $list in $web.Url
         }
       }
    }
    Write-Host
  }
}

Note: If you still cannot delete the Content Type, be sure to check your Recycle Bin for any remaining items which may be attached to that Content Type. Be sure to delete items from the End User Recycle Bin as well as the Site Collection Recycle Bin.

*Credit Shall Be Awarded To: http://blogs.msdn.com/b/david/archive/2011/08/25/powershell-script-to-locate-content-type-useage.aspx

No comments:

Post a Comment