Welcome to EMC Consulting Blogs Sign in | Join | Help

Matt Lally's Blog

SharePoint 2010 and the Usage BLOB

I had a client who wanted to show a list of all site collections and sort them by most popular using site hits.  I was able to use Martin Kearn’s SharePoint 2010 solution on Codeplex (here) to return the aggregated site collection list however Martin’s solution wasn’t grabbing site hits even though there is a method within his code that appears to do that – I confirmed that it doesn’t work.

I then came across the solution in the SharePoint Diaries Blog, in the post where Diego Bruno Galeota documents Parsing the SharePoint Usage BLOB and provides the code.  Site hits are collected if you have SharePoint’s Web Analytics enabled where the analytics is stored as a BLOB in the database – so to get site hits out of that BLOB, you have to parse it – which is what Diego’s code does.  I deployed Diego’s code and it works for SharePoint 2010.  However, one update I made to Diego’s code was to change the GetUsageBlob function in the UsageBlob.cs class to remove the NetworkCredential property (snippet below).  Martin’s SharePoint directory solution is running at the farm level using elevated credentials so it works without having to hard code credentials.

UsageBlob.cs Snippet
  1. //public static UsageBlob GetUsageBlob(string url, bool currentBlob, NetworkCredential credential)
  2. public static UsageBlob GetUsageBlob(string url, bool currentBlob)
  3. {
  4.     var uri = new Uri(url);            
  6.     ....

Then, using Martin’s Site Directory Solution I was able to add the Site Hits to the Site Directory list with the following function:

ScanJobHelperMethods.cs Snip
  1. public int GetHitsCount(SPWeb webInSite)
  2. {
  3.     int intHits = 0;
  4.     GlobalMethods globalMethods = new GlobalMethods();
  5.     System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
  6.     try
  7.     {
  8.         string thisUrl = webInSite.Url.ToString();
  9.         var blob = UsageBlob.GetUsageBlob(thisUrl, true);
  10.         var now = DateTime.Now;
  11.         var minDate = new DateTime(now.Year, now.Month, now.Day);
  12.         var browsers = from b in blob.Browsers.UsageRecords
  13.                         select new
  14.                         {
  15.                             Name = b.Key,
  16.                             Hits = (
  17.                                     from v in b.Values
  18.                                     where v.Date <= minDate
  19.                                     select v.Count
  20.                                     ).Sum(),
  21.                             Percentage = 0
  22.                         };
  23.         var totalHits = browsers.Sum(b => b.Hits);
  24.         if (totalHits > 0)
  25.         {
  26.             browsers = from b in browsers
  27.                         orderby b.Hits descending
  28.                         select new
  29.                         {
  30.                             Name = b.Name,
  31.                             Hits = b.Hits,
  32.                             Percentage = (int)Math.Round(((double)b.Hits / totalHits) * 100)
  33.                         };
  34.         }
  35.         intHits = totalHits;
  36.     }
  37.     catch (Exception ex)
  38.     {
  39.         globalMethods.WriteULSEntry(ex, TraceSeverity.Unexpected, null);
  40.     }
  41.     return intHits;  
  42. }
Published Friday, December 30, 2011 3:59 PM by matt.lally

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS



SPDevLex said:

Hi Matt,

You did absolutely fabulous R&D, I hope your solution working fine even today. I have similar assignment, did lot's for googling, but can't find any most applicable solution of my problem like yours solution. I want to deploy and test it on my dev, could you please email me your solution source code for testing or provide a bit more information about it. Thanks a lot!

November 27, 2012 4:57 PM

Leave a Comment

Powered by Community Server (Personal Edition), by Telligent Systems