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

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?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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
   }
}

SOURCE

share|improve this answer
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.