HOW TO: Retrieve Web Analytics report data using API

This blog post is a contribution from Aaron Miao, an engineer with the SharePoint Developer Support team.

In a scenario that you want to render Web Analytics report data to your favorite format, you need to retrieve the report data. There’s no document on how to retrieve SharePoint built-in report data. Some online articles are suggesting retrieving data directly from Web Analytics reporting database. It’s not recommended. Even with this approach, often customer faces issue of getting AggregationId which is required to retrieve data from database.

Here’s the sample code that can be used to retrieve Web Analytics report data.

using Microsoft.Office.Server.WebAnalytics.Reporting;
AnalyticsReportFunction ar = new AnalyticsReportFunction();  
object[,] result = ar.GetWebAnalyticsReportData(
                "2",                            // Report level,
                "TopPageForPageReport",         // Report name, 
                DateTime.Today.AddDays(-30.0),  // Start date,
                DateTime.Today);                // End date,

In the above, the Report level parameter is shown below:


And the Report name parameter is shown below:


Here’s what is returned


And here’s what you see from UI (WA report)


Comments (22)

  1. PaulE says:

    Unfortunately, wasn’t able to get this working with SummaryReport on 2010. Always got the “viewName” error. Anyone had success?

  2. Nice post, I'll need to try this out!

  3. Anonymous says:

    Nice Post. Do you know the client side API for Usage Analytic report data in SharePoint 2013 ?  I have a requirement to show the page views in custom developed apps. let me know your views if you can.

  4. MadKidDev says:

    I have a question, is it possible to retrieve the same data as the "GetSearchReport" method, only via the client side? Im working on an office 365 instance of sharepoint 2013 – using Napa as my remote dev environment and trying to pull a list of the most
    searched terms by month.

  5. ahelper says:

    Here’s an example for SharePoint 2013:

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using Microsoft.Office.Server.Search.Administration;
    using Microsoft.Office.Server.Search.Analytics;

    SPSite site = new SPSite("http://yourserver/sites/testsite/");
    SPUserToken userToken = site.UserToken;

    using (SPSite site1 = new SPSite(site.ID, userToken))
    SPServiceContext context = SPServiceContext.GetContext(site1);
    SearchServiceApplicationProxy searchProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;
    AnalyticsItemData usageData = searchProxy.GetRollupAnalyticsItemData((int)SearchReportTypeId.TopQuery, Guid.Empty, Guid.Empty, Guid.Empty);

    int dailyHits = usageData.GetHitCountForDay(DateTime.Today);
    int monthlyHits = usageData.GetHitCountForMonth(DateTime.Today);
    Console.WriteLine(String.Format("Report Daily Hits={0}; Monthly Hits={1}", dailyHits, monthlyHits));

    uint maxRows = 1000;
    DateTime startDate = new DateTime(2014, 1, 24);
    IList searchResults = searchProxy.GetSearchReport((int)SearchReportTypeId.TopQuery, Guid.Empty, Guid.Empty, startDate.Date, true, maxRows);
    if (searchResults.Count > 0)
    foreach (QueryReportData rptData in searchResults)
    int hitsCnt = rptData.Count;
    string qry = rptData.Query;
    Guid scope = rptData.Scope;
    Console.WriteLine(String.Format("Report Count={0}; Query={1}; Scope={2}", hitsCnt, qry, scope.ToString()));

    SearchReportTypeId enumeration details are here:

  6. ibulut says:

    your post is very good .

    i have a question, how to get FAST Search Connector Crawl Log  or History?

    however the  CrawlLog Class methods is obsoleted , please help if you have any answer .

  7. aaron says:

    Hi raam,

    Web Analytics has been changed in SharePoint 2013 significantly. Please check out the followings articles for details. Haven't got chance to research how you can bring those limited reports in an app.…/ff607742.aspx…/jj219554.aspx

  8. suchithra says:

    nice post,
    i need to get same in sharepoint2013,if u have code pls share.
    i know this feature is removed in Sp2013,it is merged with search service application.
    but i m not knowing how to get in SP2013.
    i tried below linked post but no use

  9. Anonymous says:

    Web Analytics Reports in SharePoint 2010

  10. Andy says:

    I also get the “viewName” error when accessing the SummaryReport. I’m tried the fixes described above

  11. Abed says:

    "Viewname" exception is always thrown when the report level and the report name is not compatible to each other.

  12. Sushma says:


    i am trying to find that index of each report where we can find exactly value.
    for example: as you described in above example about this report name "TopPageForPageReport" . so you can see in quick watch of result array that:
    [0,0] index of array shows the page url.
    [0,1] index of array shows the Site collection name.
    [0,2] index of array shows the exact value of number of page views
    [0,3] index of array — i dont have any idea about this index value.

    so i want the complete information of all the reports that which field is showing what data.

  13. MM says:

    I am trying to use the code above as mentioned, I am getting "the site not found at url http://urlofthesite" for result[0,0].. has anyone faced this issue?

  14. ArTrodes says:

    Hi, i can´t get data. All the time return in first parameter object[0,0] => "Value cannot be null.rnParameter name: identityName". What did i wrong?

  15. Akshay says:

    same as MM I am trying to use the code above as mentioned, I am getting "the site not found at url http://urlofthesite" for result[0,0].. has anyone faced this issue?

    Anyone found a solution?

  16. Akshay says:

    found it !! The console application i was using was targeted to x86 rather than x64

  17. Medes says:

    Is it possible in SP 2013 get same data using CSOM ?

  18. Ramya says:

    i tried the same code, but its always the result showing null while debugging, please help me how to solve that

  19. Kevin HIrst says:

    Can I get this DLL in the SDK? Or does anything using it have to run on the same computer that SP2010 is installed on? I installed the 2010 SDK, but didn’t see it.

  20. Anon says:

    What language is this?

  21. Leandro Quemada says:

    Is this possible for CSOM (.Net/Jquery) as well?