Friday, January 23, 2015

PowerShell to find SharePoint Content Types Usuage

PowerShell to find SharePoint Content Types Usuage:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$webs = get-spsite http://yourserver/sites/yoursite | get-spweb 


foreach ($web in $webs) 
{
  foreach ($lst in $web.lists) 
  { 
    foreach ($ctype in $lst.ContentTypes) 
    { 
      if ($ctype.Name -eq "Documents") 
      { $lst.DefaultViewUrl }
    }
  } 
  $web.Dispose() 
}


C#
Here’s a C# version to find all lists that support a particular content type:

using System;
using Microsoft.SharePoint;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSite site = new SPSite(http://yourserver/sites/yoursite);
            
            SPWebCollection webs = site.AllWebs;
            foreach (SPWeb web in webs) { 
                foreach (SPList lst in web.Lists) { 
                    foreach (SPContentType ctype in lst.ContentTypes) { 
                        if (ctype.Name == "Document") {
                            Console.WriteLine(lst.DefaultViewUrl);
                        } 
                    }
                }
                web.Dispose();
            }
            site.Dispose();
        }
    }

}

No comments:

Post a Comment