12

I have an content type I would like to delete, but when I try I get an error message saying that it is still in use. Is there a way that I can find all documents that are still using that content type?

3 Answers 3

22

Here is the code to do this using Powershell:

$site = Get-SPSite("your-site-url");
foreach ($web in $site.AllWebs) {
   $ctype = $web.ContentTypes["Your Content Type"]
   $usages = [Microsoft.Sharepoint.SPContentTypeUsage]::GetUsages($ctype)
   foreach ($usage in $usages) {
      Write-Host $usage.Url
   }
}
1
  • It works in a single farm server. But it doesn't work on another App server. What could be the issue?
    – Strider
    Commented Apr 2, 2014 at 5:19
6

I needed to do this same thing today. Get a list of SPContentTypeUsage's that define where the items are being used. If the Check if the URL is to a list (such as a Pages library), then run an SPQuery to get those SPListItem's that match your content type. You'll need to iterate over them in a for loop to delete them.

SPContentType ct = site.RootWeb.ContentTypes["CustomContentType"];

IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(ct);

foreach (SPContentTypeUsage usage in usages)
{

    if (usage.IsUrlToList)
    {
        SPList list = web.GetList(usage.Url);
        SPQuery query = new SPQuery();
        query.Query = string.Concat(
                        "<Where><Eq>",
                            "<FieldRef Name='ContentType'/>",
                            string.Format("<Value Type='Text'>{0}</Value>", ct.Name),
                        "</Eq></Where>");
        SPListItemCollection listItems = list.GetItems(query);
        for (int i = 0; i < listItems.Count; i++)
        {
            listItems[i].Delete();
        }
    }
}
1
  • perfect, saved me 30 minutes of investigating. Thanks. Commented Nov 21, 2012 at 9:08
3

You can use code to delete the content type or report an error stating where it is being used.

Please refer to this simliar question.

2
  • So it can only be done through code? Commented Jun 14, 2011 at 21:18
  • You could use Powershell. I'll write that as a separate answer so it is more readable.
    – Laurie
    Commented Jun 14, 2011 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.