Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers 3

up vote 15 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
   }
}
share|improve this answer
    
It works in a single farm server. But it doesn't work on another App server. What could be the issue? –  Strider Apr 2 at 5:19
add comment

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();
        }
    }
}
share|improve this answer
    
perfect, saved me 30 minutes of investigating. Thanks. –  Brian Scott Nov 21 '12 at 9:08
add comment

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.

share|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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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