Search code examples
c#sharepointsharepoint-2007

SharePoint get the size of individual web sites using API


Right now all I am using to calculate the size are the files in the folders. I do not think this is all of it, because the content database size is about 15gb. When I calculate the size of all the files I get around 10gb. Does anyone know what I may be missing?

Here is the code I have so far.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Globalization;

namespace WebSizeTesting
{
class Program
{


    static void Main(string[] args)
    {
        long SiteCollectionBytes = 0;

        using (SPSite mainSite = new SPSite("http://sharepoint-test"))
        {
            // loop through the websites
            foreach (SPWeb web in mainSite.AllWebs)
            {

                long webBytes = GetSPFolderSize(web.RootFolder);

                // Add in size of each web site's recycle bin

                webBytes += web.RecycleBin.OfType<SPRecycleBinItem>().Select(item => item.Size).ToArray<long>().Sum();

                Console.WriteLine("Url: {0}, Size: {1}", web.Url, ConvertBytesToDisplayText( webBytes ));

                SiteCollectionBytes += webBytes;
            }


            long siteCollectionRecycleBinBytes = mainSite.RecycleBin.OfType<SPRecycleBinItem>().Select(item => item.Size).ToArray<long>().Sum();

            Console.WriteLine("Site Collection Recycle Bin: " + ConvertBytesToDisplayText(siteCollectionRecycleBinBytes));

            SiteCollectionBytes += siteCollectionRecycleBinBytes;
        }

        Console.WriteLine("Total Size: " + ConvertBytesToDisplayText(SiteCollectionBytes));

        Console.ReadKey();
    }



    public static long GetSPFolderSize(SPFolder folder)
    {
        long byteCount = 0;

        // calculate the files in the immediate folder
        foreach (SPFile file in folder.Files)
        {
            byteCount += file.TotalLength;

            // also include file versions
            foreach (SPFileVersion fileVersion in file.Versions)
            {
                byteCount += fileVersion.Size;
            }

        }

        // Handle sub folders
        foreach (SPFolder subFolder in folder.SubFolders)
        {
            byteCount += GetSPFolderSize(subFolder);
        }

        return byteCount;
    }

    public static string ConvertBytesToDisplayText(long byteCount)
    {
        string result = "";

        if (byteCount > Math.Pow(1024, 3))
        {
            // display as gb
            result = (byteCount / Math.Pow(1024, 3)).ToString("#,#.##", CultureInfo.InvariantCulture) + " GB";
        }
        else if (byteCount > Math.Pow(1024, 2))
        {
            // display as mb
            result = (byteCount / Math.Pow(1024, 2)).ToString("#,#.##", CultureInfo.InvariantCulture) + " MB";
        }
        else if (byteCount > 1024)
        {
            // display as kb
            result = (byteCount / 1024).ToString("#,#.##", CultureInfo.InvariantCulture) + " KB";
        }
        else
        {
            // display as bytes
            result = byteCount.ToString("#,#.##", CultureInfo.InvariantCulture) + " Bytes";
        }

        return result;
    }
}
}

edit 2:15 pm 3/1/2010 cst I added in the ability to count file versions as part of the size to the code. As was suggested by Goyuix in the post below. It still is off by a considerable amount of the physical database size.

edit 8:38 am 3/3/2010 cst I added in the calculating of the recycle bin size for each web, and the site collection recycle bin. These changes where suggested by ArjanP. Also i wanted to add, that I am very open to more efficient ways of doing this.


Solution

  • Did you consider the Trash Can? There will be cans for Webs and the Site Collection, all taking up space in the content database.

    There will always be 'overhead' in a content database.. every 'empty' Web will consume a number of bytes already. 30% seems much but not excessive, it depends on the ratio of content and the number of webs.