Sunday, February 19, 2012

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 Axceler Control Point 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: http://versioningsizereport.codeplex.com/ .

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



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

9 comments :

  1. 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.

    ReplyDelete
    Replies
    1. Jong, I've Created a Project in CodePlex for this utility. You can download source code and executable from here. http://versioningsizereport.codeplex.com/

      Delete
  2. Thanks for this great post

    http://csharpektroncmssql.blogspot.com

    ReplyDelete
  3. 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?

    ReplyDelete
    Replies
    1. Go to event viewer (Start > Run > Eventvwr), under Application node, check for errors logged with source "Get Versioning Report" to get more insight!

      Delete
  4. 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

    ReplyDelete
    Replies
    1. 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!

      Delete
  5. 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

    ReplyDelete
    Replies
    1. Chuck,

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

      Delete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...