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?

link|improve this question

feedback

3 Answers

up vote 8 down vote accepted

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
   }
}
link|improve this answer
feedback

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.

link|improve this answer
So it can only be done through code? – Abe Miessler Jun 14 '11 at 21:18
You could use Powershell. I'll write that as a separate answer so it is more readable. – Laurie Jun 14 '11 at 22:06
feedback

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();
        }
    }
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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