Version History Size Report for Entire SharePoint Web Application

Ever wanted to get a report on versioning sizes on SharePoint? Unlimited No. of versions enabled in various document library across web application. As part of regular auditing, wanted to generate report for the entire web application ,with Number of versions, Amount of storage consumed by versions. Of course there are some third-party products like Metalogix ControlPoint with additional capabilities. But How about wring my own code? Sounds good.

Here is my code: Tested in SharePoint 2010 and MOSS 2007.

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

namespace SharePointReporting
{
    class GetVersioningReport
    {
        static void Main(string[] args)
        {
            string site;
            StreamWriter SW;
            try
            {
                if (args.Length == 0)
                {
                    Console.WriteLine("Enter the Web Application URL:");
                    site = Console.ReadLine();
                }
                else
                {
                    site = args[0];
                }

                SPSite tmpRoot = new SPSite(site);
                SPSiteCollection tmpRootColl = tmpRoot.WebApplication.Sites;

                //objects for the CSV file generation
                SW = File.AppendText("c:\\VersioningReport.csv");

                //Write the CSV Header
                SW.WriteLine("Site Name, Library, File Name, File URL, Last Modified, No. of Versions, Latest Version Size -KB,Total Versions Size - MB");

                //Enumerate through each site collection
                foreach (SPSite tmpSite in tmpRootColl)
                {
                    //Enumerate through each sub-site
                    foreach (SPWeb tmpWeb in tmpSite.AllWebs)
                    {
                        //Enumerate through each List
                        foreach (SPList tmpList in tmpWeb.Lists)
                        {
                            //Get only Document Libraries & Exclude specific libraries
                            if (tmpList.BaseType == SPBaseType.DocumentLibrary &  tmpList.Title!="Workflows" & tmpList.Title!= "Master Page Gallery" & tmpList.Title!="Style Library" & tmpList.Title!="Pages")
                            {
                                  foreach (SPListItem tmpSPListItem in tmpList.Items)
                                    {

                                        if (tmpSPListItem.Versions.Count > 5)
                                        {
                                            
                                            SPListItemVersionCollection tmpVerisionCollection = tmpSPListItem.Versions;

                                            //Get the versioning details
                                            foreach (SPListItemVersion tmpVersion in tmpVerisionCollection)
                                             {
                                                int versionID = tmpVersion.VersionId;
                                                string strVersionLabel = tmpVersion.VersionLabel;
                                             }

                                            //Get the versioning Size details
                                            double versionSize = 0;
                                            SPFile tmpFile = tmpWeb.GetFile(tmpWeb.Url + "/" + tmpSPListItem.File.Url);

                                            foreach (SPFileVersion tmpSPFileVersion in tmpFile.Versions)
                                            {
                                                versionSize = versionSize + tmpSPFileVersion.Size;
                                            }
                                            //Convert to MB
                                            versionSize= Math.Round(((versionSize/1024)/1024),2);

                                            string siteName;
                                            if (tmpWeb.IsRootWeb)
                                            {
                                                siteName= tmpWeb.Title +" - Root";
                                            }
                                            else
                                            {
                                                siteName=tmpSite.RootWeb.Title + " - " + tmpWeb.Title;
                                            }

                                            //Log the data to a CSV file where versioning size > 0MB!
                                            if (versionSize > 0)
                                            {
                                                SW.WriteLine(siteName + "," + tmpList.Title + "," + tmpSPListItem.Name + "," + tmpWeb.Url + "/" + tmpSPListItem.Url + "," + tmpSPListItem["Modified"].ToString() + "," + tmpSPListItem.Versions.Count + "," + (tmpSPListItem.File.Length / 1024) + "," + versionSize );
                                            }
                                        }
                                   }
                               }
                          }
                      }
                 }

                //Close the CSV file object 
                SW.Close();

                //Dispose of the Root Site Object
                tmpRoot.Dispose();
                
                //Just to pause
                Console.WriteLine(@"Versioning Report Generated Successfull at c:\VersioningReport.csv. Press ""Enter"" key to Exit");
                Console.ReadLine();
            }

            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Get Versioning Report", ex.Message);
            }
        }
    }
}

After adding headers and Pivot tables to the CSV file via Excel, it was fantastic Report!! You can get Individual versions ID, Lables, Size information by slightly changing the code.

SharePoint Version History Size Report

Add a pivot table to make the SharePoint version history report little more meaningful.

sharepoint version history report

I’ve created a Project in CodePlex for this utility:

For PowerShell version of the above code, Refer to my another post: SharePoint Document Versions Size Report with PowerShell

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

9 thoughts on “Version History Size Report for Entire SharePoint Web Application

  • Hi Salaudeen,

    I’ve downloaded your code and it is exactly what I need. However it appears to be listing only document with more the 5 versions. I don’t have the ability to modify/recompile the code to remove this limitation. Would it be possible for you to do that and send me a exe?

    That would be greatly appreciated!

    Thanks in advance!
    Chuck
    chuck.carroll@brooks.com

    Reply
    • Chuck,

      The CodePlex project has the Source code as well as executable. You can use the PowerShell code as well.

      Reply
  • Hello Salaudeen,
    i have downloaded your solution but i can find a way to generate the CSV file as you have in your screenshot. could you please provide the steps on how to do that. thanks

    Reply
    • This executable will generate a csv file at “c:VersioningReport.csv”, which is provided in first screenshot. For second screen, just open the csv file in Microsoft Excel and add a Pivot table!

      Reply
  • Hi,

    I am trying to run the .exe file in sharepoint foundation 2010. it creates an empty file VersioningReport.csv. and then finish. not even the header “Site Name, Library, File Name…..” are created.

    we have a windows standard 2008R2 with sharepoint foundation 2010.
    I run it as administrator of the system and the web application.

    do you have and idea what the problem could be?

    Reply
    • Go to event viewer (Start > Run > Eventvwr), under Application node, check for errors logged with source “Get Versioning Report” to get more insight!

      Reply
  • Thanks for this great post

    https://csharpektroncmssql.blogspot.com

    Reply
  • Truly great real world example – I think got managing growth of the contentDB it is excellent to give insight in versioning.

    I wanted to drop de code in powershell or visual studio but it copies in one straight line and gives errors on parenthesis closing .. Would you be so kind to share your code in another format that has carriage return etc.

    Reply
    • Jong, I’ve Created a Project in CodePlex for this utility. You can download source code and executable from here. https://versioningsizereport.codeplex.com/

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *