SharePoint 2010: How to List Out Webparts Used in Publishing Site Pages - TechNet Articles - United States (English) - TechNet Wiki

I got a request from someone to provide a way to enlist webparts used in all publishing pages of a publishing site.
After some initial work I came with this solution. This solution takes a oter">

SharePoint 2010: How to List Out Webparts Used in Publishing Site Pages

I got a request fromcommand line parameter that is the URL of the site from where you want to get the data. There is no dependency of library name used to store these pages. It will enlist all publishing pages and corresponding webparts.


Remember :- A command line parameter has to be given as site url to invoke this tool otherwise this will fail.

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using Microsoft.SharePoint;
 using Microsoft.SharePoint.Publishing;
 using Microsoft.SharePoint.WebPartPages;
 using System.Web.UI.WebControls.WebParts;
  
 
namespace ListOutWebParts
 {
     class Program
     {
         static void Main(string[] args)
         {
             Console.WriteLine("This tool will enlist the webparts");
             String siteURL = args[0];
             try
             {
                 using (SPSite site = new SPSite(siteURL))
                 {
                     using (SPWeb web = site.OpenWeb())
                     {
                         PublishingWeb webPublish = PublishingWeb.GetPublishingWeb(web);
                         PublishingPageCollection pages = webPublish.GetPublishingPages();
                         foreach (PublishingPage page in pages)
                         {
                             SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(page.Url, PersonalizationScope.Shared);
                             SPLimitedWebPartCollection webCollection = manager.WebParts;
                             if (webCollection.Count != 0)
                             {
                                 Console.WriteLine("The page " + page.Title + " contains these webparts");
                                 "The page " + page.Title + " contains these webparts");
                  &nbsfont-weight:bold;">for (int i = 0; i < webCollection.Count; i++)
                                 {
                                     Console.WriteLine((i + 1).ToString() + " " + webCollection[i].GetType().Name + " " + webCollection[i].Title);
                                 }
                             }
                             
                         }
                                            }
                 }
             }
             catch (Exception e)
             {
                 Console.WriteLine(e);
             }
         }
     }
    
 }


I am showing the sample output:

This tool will enlist the webparts
The page Home contains these webparts
1 ContentByQueryWebPart Press Releases
The page Xslttest contains these webparts
1 ListViewWebPart Content and Structure Reports
2 DataFormWebPart Content and Structure Reports (1)

Hence first it displays the page title then in the serial order it lists first type of the webpart and then the title as used in that page.