Monday, 25 May 2015

How to find all document of particular content type

We can use below powershell to find documents of certain content type:

 $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  

To find it using code you can below lines of code

 using (SPSite siteCollection = new SPSite("http://localhost"))  
       using (SPWeb webSite = siteCollection.OpenWeb())  
         // Get the content type.  
         SPContentType obsolete = webSite.ContentTypes["Test"]; 
         // We have a content type.  
         if (obsolete != null)   
          IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(obsolete);  
          // It is in use.  
          if (usages.Count > 0)   
            Console.WriteLine("The content type is in use in the following locations:");  
            foreach (SPContentTypeUsage usage in usages)  
          Console.WriteLine("The content type does not exist in this site collection.");  
      Console.Write("\nPress ENTER to continue...");  

Hope this helps.!!!!!!!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...