Generate a report about all the sites and lists under a site collection

One of my customer wants monitors their SharePoint environment in order to meet governance requirement.

Primary goal was generate a report of site hierarchy under a site collection including webs and libraries etc. created during specific timeframe.

Thus, I have created a small utility – a .NET console based application which will accept a site collection URL as an input parameter and generate report which will dump the information name and created date of sites and lists.

Here is the code snippet.

    1: using System;
    2: using System.Collections.Generic;
    3: using System.Text;
    4: using Microsoft.SharePoint;
    5: using System.IO;
    6:  
    7: namespace SiteInformation
    8: {
    9:     class Program
   10:     {
   11:         static TextWriter reportfile = null;
   12:  
   13:         static void Main(string[] args)
   14:         {
   15:             try
   16:             {
   17:                 reportfile = new StreamWriter("SiteInfo.csv");
   18:  
   19:                 Console.WriteLine("Enter your site collection URL :");
   20:  
   21:                 string strUrl = Console.ReadLine();
   22:  
   23:                 SPSecurity.RunWithElevatedPrivileges(delegate()
   24:                 {
   25:                     using (SPSite oSite = new SPSite(strUrl))
   26:                     {
   27:                         using (SPWeb oRootWeb = oSite.OpenWeb())
   28:                         {
   29:                             SPWebCollection oWebs = oRootWeb.Webs;
   30:                             if (oWebs.Count == 0)
   31:                                 reportfile.WriteLine("No subsites under " + oRootWeb.Name);
   32:                             else
   33:                                 GetData(oWebs);
   34:                         }
   35:                     }
   36:                 });
   37:  
   38:             }
   39:             catch (Exception ex)
   40:             {
   41:                 Console.WriteLine(ex.Message);
   42:             }
   43:             finally
   44:             {
   45:                 reportfile.Close();
   46:                 reportfile.Dispose();
   47:             }
   48:             Console.WriteLine("Operation completed successfully !");
   49:             Console.ReadLine();
   50:         }
   51:  
   52:           static void GetData(SPWebCollection oWebs)
   53:           {
   54:               foreach (SPWeb oWeb in oWebs)
   55:                   {
   56:                       reportfile.WriteLine("*********************************************************");
   57:                       reportfile.WriteLine("Site URL " + oWeb.Url);
   58:                       reportfile.WriteLine("Site Name >> " + oWeb.Name + " || Created Date >> " + oWeb.Created.ToString());
   59:                       GetLists(oWeb.Lists);
   60:  
   61:                     if (oWeb.Webs.Count == 0)
   62:                         reportfile.WriteLine("No subsites under " + oWeb.Name);
   63:                     else
   64:                         GetData(oWeb.Webs);                  
   65:                   }
   66:           }
   67:  
   68:         static void GetLists(SPListCollection oLists)
   69:         {
   70:             foreach (SPList oList in oLists)
   71:             {
   72:                 reportfile.WriteLine("List Name >> " + oList.Title + " || Created Date >> " + oList.Created.ToString());
   73:             }
   74:         }
   75:     }
   76: }

I have attached the application in the format of a zip file with this post.

SiteInformation.zip