using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Web;
using System.Text.RegularExpressions;
using System.Data;
using System.Collections;


namespace GetAllWebsSize
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This tool is designed for finding which document library has enabled history version in all  websites and sites");

            InitionFile();
            Console.WriteLine("Please input the web url:");
            String weburl = Console.ReadLine ();
            Console.WriteLine("Please input Version num:");
            String getVerNumString = Console.ReadLine ();
            Int16 getVerNum=Convert.ToInt16(getVerNumString );

            Uri webAppUri = new Uri(weburl);
            SPWebApplication webApplication = SPWebApplication.Lookup(webAppUri);

            Console.WriteLine("WebURL\tDocumentLibrary(MB)\tLastModifiedTime\tSiteOwner\tMajorVersionLimit\tMinorVersionsLimit\tSize");
            writer.WriteLine("WebURL\tDocumentLibrary(MB)\tLastModifiedTime\tSiteOwner\tMajorVersionLimit\tMinorVersionsLimit\tSize");


            foreach (SPSite site in webApplication.Sites)
            {            
                 DataTable oDtRawData = null;
                oDtRawData = site.StorageManagementInformation(SPSite.StorageManagementInformationType.DocumentLibrary, SPSite.StorageManagementSortOrder.Decreasing, SPSite.StorageManagementSortedOn.Size, 0);
                foreach (SPWeb web in site.AllWebs)               
                {                     
                    foreach (SPList list in web.Lists  )
                    {
                        string listId = "{" + list.ID.ToString() + "}";
                         foreach (DataRow oRow in oDtRawData.Rows)
                        {
                            string oRowListGuid = oRow["ListGuid"].ToString().ToLower();

                            if (list.EnableVersioning.ToString ()=="True"&&oRowListGuid == listId.ToLower() && list.BaseTemplate.ToString() == "DocumentLibrary" && list.Hidden.ToString() != "True" && list.EnableVersioning.ToString() == "True" && list.Title != "Site Collection Documents" && list.Title != "Style Library   False" && list.Title != "Site Collection Images" && list.Title != "Style Library")
                            {
                                if (list.MajorVersionLimit == 0|| list.MajorVersionLimit >= getVerNum || list.MajorWithMinorVersionsLimit >= getVerNum)
                                {
                                    string minorVer = null;
                                    if (list.EnableMinorVersions.ToString() == "False") minorVer = "Disable";
                                    else minorVer = list.MajorWithMinorVersionsLimit.ToString();
                                    try
                                    {
                                        foreach (SPUser user in web.AssociatedOwnerGroup.Users)
                                        {                                          
                                            Console.WriteLine("http://" + webAppUri.Host + list.ParentWebUrl.ToString() + "\t" + list.Title + "\t" + list.LastItemModifiedDate.ToString() + "\t" + user.LoginName + "\t" + list.MajorVersionLimit.ToString() + "\t" + minorVer + "\t" + oRow["Size"].ToString());
                                            writer.WriteLine("http://" + webAppUri.Host + list.ParentWebUrl.ToString() + "\t" + list.Title + "\t" + list.LastItemModifiedDate.ToString() + "\t" + user.LoginName + "\t" + list.MajorVersionLimit.ToString() + "\t" + minorVer + "\t" + oRow["Size"].ToString());

                                        }
                                    }
                                    catch
                                    {
                                        writer.WriteLine("http://" + webAppUri.Host + list.ParentWebUrl.ToString() + "\t" + list.Title + "\t" + list.LastItemModifiedDate.ToString() + "\t" + "access error" + "\t" +  list.MajorVersionLimit.ToString() + "\t" + minorVer + "\t" + oRow["Size"].ToString());
                                    }
                                }
                            }                           
                        }                      
                    }
                }
            }
            CloseFile();
           // Console.ReadKey();
        }

        static StreamWriter writer;
        static void InitionFile()
        {
            writer = new StreamWriter("HistoryVer.txt", false);
        }
        static void CloseFile()
        {
            writer.Flush();
            writer.Close();
        }

        static void WriteLine(string msg)
        {
            writer.WriteLine(msg);
        }
    }
}