Is there a quick powershell script to find out all the consumers who are consuming a given content type? If yes, how do you do this with powershell?
2 Answers
I thought I had to do this at one point, wrote some code, but never ran it. Here's what I came up with, and you may be able to modify it to your needs.
It finds all the SPContentTypeUsage
's for a given content type. In my example, I'm going out and deleting all list items that are of my content type.
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();
}
}
}
Edit
You wanted powershell. This might get you what you want. Credit goes to Laurie
$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
}
}
Using PowerShell
Find the usages of the content type.
$w = Get-SPWeb "http://yout/web/url" $ct = $w.ContentTypes | Where { $_.ID -eq "[content type id]" } [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($ct);
Go to those places and see the items that are referencing the content type