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
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
}
}
-
It works in a single farm server. But it doesn't work on another App server. What could be the issue?– StriderCommented Apr 2, 2014 at 5:19
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();
}
}
}
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.
-
-
You could use Powershell. I'll write that as a separate answer so it is more readable.– LaurieCommented Jun 14, 2011 at 22:06