Wednesday, September 26, 2012

SharePoint – How to get Site Usage Information Using Client Object Model

Getting site usage information using client object model is very simple.
We have to load “Usage” property of site and use “UsageInfo” class provided in client object model.
Data type of Usage property is UsageInfo. We can get information related to usage like hits, site visits and also storage for the site.
Here I have written sample method which will give us required usage data.
private void GetSiteUsageData()
        {
            string usageData = string.Empty;
            clientContext.Load(clientContext.Site,
            s => s.Usage);
            clientContext.ExecuteQueryAsync((s, args) =>
            {
                UsageInfo usageInfo = clientContext.Site.Usage;
                usageData = "No. of hits: " + Convert.ToString("" + usageInfo.Hits) + "; ";
                usageData += "No. of visits: " + Convert.ToString("" + usageInfo.Visits) + "; ";
                usageData += "Storage: " + Convert.ToString("" + usageInfo.Storage) + "; ";
                usageData += "Storage Percentage Used: " + Convert.ToString("" + usageInfo.StoragePercentageUsed) + "; ";
            },
            (s, args) =>
            {
                //"Error loading usage information.
            });           
        }
 
Here are the details of properties,
 
Property Name
Data Type
Description
Hits
Long
Gives total number of hits to the site
Visits
Long
Gives number of page visits
Storage
Long
Gives total storage used in MB
StoragePercentageUsed
Double
Gives percentage of storage used by site
 
This is handy when you want to get usage information using client object model.

4 comments:

  1. ExecuteQueryAsync is not a method of ClientContext.....

    ReplyDelete
  2. I am getting zero hit while using UsageInfo

    ReplyDelete
  3. ExecuteQueryAsync is for JSOM.
    Use ExecuteQuery() in C# and then like below:

    sourceClientContext.Load(sourceClientContext.Site, s => s.Usage);
    sourceClientContext.ExecuteQuery();
    UsageInfo usageInfo = sourceClientContext.Site.Usage;
    var bytes = usageInfo.Storage

    ReplyDelete