Simple console application program to calculating Size of Site Collection, Sub Sites in SharePoint
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace CalculatingSPSitesSize
{
class Program
{
static void Main(string[] args)
{
long siteCollectionSize = 0;
string baseUrl = "http://Portal";
Console.WriteLine("Base
Url: " + baseUrl + " (Change
baseUrl to list sites starting with)");
using (SPSite
mainSite = new SPSite(baseUrl))
{
foreach (SPWeb
web in mainSite.AllWebs)
{
long webSize =
GetSPFolderSize(web.RootFolder) + web.RecycleBin.Cast<SPRecycleBinItem>().Sum(r => r.Size);
if (web.Url.StartsWith(baseUrl))
{
Console.WriteLine(string.Format("({0} {1}", web.Url,
FormatSize(webSize)));
siteCollectionSize +=
webSize;
}
}
}
Console.WriteLine("Total
Size: " + FormatSize(siteCollectionSize));
Console.ReadKey(false);
}
public static long GetSPFolderSize(SPFolder
folder)
{
long
folderSize = 0;
foreach (SPFile
file in folder.Files)
folderSize += file.TotalLength
+ file.Versions.Cast<SPFileVersion>().Sum(f
=> f.Size);
folderSize += folder.SubFolders.Cast<SPFolder>().Sum(sf
=> GetSPFolderSize(sf));
return folderSize;
}
public static string FormatSize(long
size)
{
if (size > Math.Pow(1024,
3))
return (size / Math.Pow(1024,
3)).ToString("#,#.##") + " GB";
else if (size > Math.Pow(1024, 2))
return (size / Math.Pow(1024,
2)).ToString("#,#.##") + " MB";
else if (size >
1024)
return (size / 1024).ToString("#,#.##") + "
KB";
else
return size.ToString("#,#.##") + "
Bytes";
}
}
}
Reference
http://www.c-sharpcorner.com/UploadFile/40e97e/calculating-size-of-site-collection-sub-sites-in-multiple-w/