September 11, 2008

How to get usage information for a specific site collection by code

To get usage information for a site collection, have a look at the Usage property of the SPSite class.

This property is a structure which contains 5 fields:
- Storage: total amount of storage (bytes) consumed
- DiscussionStorage: amount of storage (bytes) used by the web discussion data
- Hits: cumalative number of visits
- Bandwidth: cumulative bandwith used
- Visits: cumulative number of visits

Note that 2 (Storage and DiscussionStorage) are updated in real time and 3 (Bandwidth, Hits and Visits) are updated daily by the usage analysis timer job.

The following console application displays the usage information for a specific site collection:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
 
namespace UsageInformation
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite siteCollection = new SPSite("http://siteCollectionUrl"))
            {
                Console.WriteLine(string.Format("SiteCollection Url: {0}", siteCollection.Url));
                Console.WriteLine(string.Format("Storage: {0}", siteCollection.Usage.Storage));
                Console.WriteLine(string.Format("Discussion Storage: {0}", siteCollection.Usage.DiscussionStorage));
                Console.WriteLine(string.Format("Bandwidth: {0}", siteCollection.Usage.Bandwidth));
                Console.WriteLine(string.Format("Hits: {0}", siteCollection.Usage.Hits));
                Console.WriteLine(string.Format("Visits: {0}", siteCollection.Usage.Visits));
 
                Console.WriteLine(string.Format("Press a touch to exit..."));
                Console.ReadLine();
            }
        }
    }
}

1 comment:

SharePoint Reporting, SharePoint Usage Reports said...

and you can also try CardioLog Lite for sharepoint usage reports - also for site collections.

Uri, Intlock.