.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Calculating Size of Site Collection, Sub Sites in multiple Ways in SharePoint

Posted By:Jean Paul       Posted Date: September 27, 2013    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this article we shall explore the Site Collection Size calculation options and tools associated in SharePoint
 

Calculating the Size of a Site Collection or Sub Sites is a periodic task in the life time 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


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

Following are the solutions to calculate Size.

Solution 1: STSADM Command

STSADM contains a command to return Size of Site Collection.
  stsadm -o enumsites -url http://Server
This command should return the number of Sites & Size for the URL specified
<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>

STSADM is deprecated and the above Operation does not yield Drill Down results.  We can continue our exploration on other possibilities.

Solution 2: Server Object Model using C#

Using Server Object Model we can write a Console Application to display more drilled down information like the size of Site, 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

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.



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

Solution 3: Using PowerShell

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.
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 over Code Plex to calculate the size of SharePoint sites.  I would like to list one of the Best Tool for this purpose.

The tool is named SharePoint Space Monitor and you can download it from:
  http://scmodsoft.com/download/
You need to register an email id for getting the registration code.  On executing the tool you can see the following screen with graphical charts and drill down options.

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.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend