Calculating Size of Site Collection, Sub Sites in Multiple Ways

Calculating the size of a Site Collection or Sub Sites is a periodic task in the lifetime of a SharePoint Deployment. The calculation is required in the following scenarios:

  1. Determining the Site Collection usage & progress for Reporting purposes
  2. Finalizing Architectural Decisions on Expansions or Splitting of Site Collections
  3. Periodical Analysis of Site Quota & Usage

Note:

The term refers to the size occupied by the entity. Please note that the Total Size calculated might be less than the Content Database Storage Size as there is surplus space allocated.

Solutions

The following are the solutions to calculate the size.

Solution 1: STSADM Command

STSADM contains a command to return the Size of a Site Collection, as in:

stsadm -o enumsites -url http://Server

This command should return the number of Sites & the size for the URL specified, as in:

<Sites Count="2">

  <Site Url="http://hp" Id="1abb65ae-5df0-491d-aaca-cff97c93a935" Owner="HP\hp" ContentDatabase="WSS_Content_10737a6d-4f76-4a8e-b7b0-047ad8c3da1e" StorageUsedMB="69.1" StorageWarningMB="0" StorageMaxMB="0" />

 <Site Url="http://hp/sites/my" Id="ad2d7c45-db2b-47d4-97e4-dfddf9ec9a93" Owner="HP\hp" ContentDatabase="WSS_Content_10737a6d-4f76-4a8e-b7b0-047ad8c3da1e" StorageUsedMB="1.7" StorageWarningMB="0" StorageMaxMB="0" /> 

</Sites>

Note:

STSADM is deprecated and the preceding operation does not yield Drill Down results. We can continue our exploration of other possibilities.

Solution 2: Server Object Model using C#

Using the Server Object Model we can write a Console Application to display more drilled down information like the size of Site and Sub Sites. Here we are including the following entities:

  1. Site

  2. Sub Sites

  3. Folders

  4. Files

  5. File Versions

  6. Recycle Bin

Code

The following is the code which performs the above activities, formats the size and outputs the result on the console:

static void Main(string[] args)

{

    long siteCollectionSize = 0;

    string baseUrl = "http://hp";

 

    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";

}

On executing the code above, you will get the following results:

Output-Size-of-Site-Collection-Sub-Sites-in-multiple-Ways.jpg

You can change the baseUrl to list only the sites starting with a value.

Solution 3: Using PowerShell

The following is the PowerShell script which performs the same functionality of C# code. As always PowerShell provides lightweight solutions without deployment & modification hassles for the IT Professionals.

The following is the PowerShell script which performs the same functionality.

# Returns size of site, subsites, folders, outputs in .\SiteSize.html

 

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

 

$rootUrl = "http://hp/wiki";

$rootSite = new-object Microsoft.SharePoint.SPSite($rootUrl);

$array = @()

$siteCollectionSize = 0;

 

foreach($web in $rootSite.AllWebs)

{

    if ($web.Url.StartsWith($rootUrl))

    {

        $websize = GetFolderSize $web.RootFolder;

       

        foreach($recycleBinItem in $web.RecycleBin)

        {

           $websize += $recycleBinItem.Size;

        }

       

        $formatSize = FormatBytes $websize;

        $url = $web.Url;

       

         # Create object and store in array

        $obj = New-Object System.Object

        $obj | Add-Member -type NoteProperty -name "Description" -value "$url"

        $obj | Add-Member -type NoteProperty -name "Size" -value "$formatSize"

        $array += $obj

       

        $siteCollectionSize += $websize

    }

}

 

$formatSize = FormatBytes $siteCollectionSize;

 

# Create object and store in array

$obj = New-Object System.Object

$obj | Add-Member -type NoteProperty -name "Description" -value "[Total Size]"

$obj | Add-Member -type NoteProperty -name "Size" -value "[$formatSize]"

$array += $obj

 

# Display

foreach($item in $array)

{

    write-output $item

}

 

# Write to HTML

$array | Select-Object | ConvertTo-Html -title "Site Size" | Set-Content .\SiteSize.html

 

# Function calculating folder size

Function GetFolderSize($folder)

{

    $filesize = 0;

 

    foreach ($file in $folder.Files)

    {

        $filesize += $file.TotalLength;

       

        foreach ($fileVersion in $file.Versions)

        {

            $filesize += $fileVersion.Size;

        }

    }

 

    foreach ($subfolder in $folder.SubFolders)

    {

        $filesize += GetFolderSize $subfolder

    }

           

    return $filesize;

}

 

# Function to format in MB

function FormatBytes ($bytes)

{

    switch ($bytes)

    {

        {$bytes -ge 1TB} {"{0:n$sigDigits}" -f ($bytes/1TB) + " TB" ; break}

        {$bytes -ge 1GB} {"{0:n$sigDigits}" -f ($bytes/1GB) + " GB" ; break}

        {$bytes -ge 1MB} {"{0:n$sigDigits}" -f ($bytes/1MB) + " MB" ; break}

        {$bytes -ge 1KB} {"{0:n$sigDigits}" -f ($bytes/1KB) + " KB" ; break}

        Default { "{0:n$sigDigits}" -f $bytes + " Bytes" }

    }

}

Solution 4: Advanced Tools

There are many tools available in CodePlex to calculate the size of SharePoint sites. I would like to list one of the best tools for this purpose.

The tool is named SharePoint Space Monitor and you can download it from:

http://scmodsoft.com/download/

You need to register with an email id to get the registration code. On executing the tool you will see the following screen with graphical charts and drill down options.

solution-Size-of-Site-Collection-Sub-Sites-in-multiple-Ways.jpg

References

http://tinyurl.com/sp2010-stusg

Summary

In this article we have explored the Site Collection Size calculation options and tools associated. I hope the information should be useful in real-world scenarios.