Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 25064

GetUsageData is always returning Null

$
0
0

Let me first start out by stating that I know there are many questions on this issue, and I have read them all. However, none of the suggestions that I have read have seemed to help my situation. Therefore, I figure I would supply my code with the hopes that someone can solve the issue that is mentioned in the title. Let me first supply a little bit of background.

My goal in this code is to determine what are the most visited sites in our SharePoint 2010 farm. To do this, I figured I would cycle through all of the webs in the farm, and then obtain the amount of page views within each site using the GetUsageData function. Just to test if the GetUsageData was supplying the correct data, I coded the web part in such a way where if GetUsageData returned "Null", it would print it for every site, and "Not Null" if otherwise. In every case I have tested this web part, it has always returned Null for evey site it has checked.

I am fairly new to programming web parts in C#, so a lot of this code is derived from what I have read when researching the subject. If anyone can assist me to see where I could fix my issue, I would greatly appreciate it.

Thank you.

Below is my Code:

using System;
using System.Data;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
//This code has been derived from the following link: http://blog.rafelo.com/2008/07/22/iterating-through-sharepoint-web-applications-site-collections-and-sites-webs/
namespace WebAnalyticsTest.Test
{
    [ToolboxItemAttribute(false)]
    public class Test : WebPart
    {
        //For testing purposes. More is explained further in the code.
        int test;
        ArrayList totalHitsArray = new ArrayList();
        int totalHits;
        DataGrid grid = null;
        public void BeginProcess() 
        {
            // Get references to the farm and farm WebService objects 
            // the SPWebService object contains the SPWebApplications 
            SPFarm thisFarm = SPFarm.Local; 
            SPWebService service = thisFarm.Services.GetValue<SPWebService>("");
            foreach (SPWebApplication webApp in service.WebApplications) 
            {
                //Execute any logic you need to against the web application 
                //Iterate through each site collection 
                foreach (SPSite siteCollection in webApp.Sites) 
                {
                    //do not let SharePoint handle the access denied  
                    //exceptions. If one occurs you will be redirected 
                    //in the middle of the process. Handle the AccessDenied 
                    //exception yourself in a try-catch block 
                    siteCollection.CatchAccessDeniedException = false; 
                    try 
                    {
                        //Execute any logic you need to against the site collection 
                        //Call the recursive method to get all of the sites(webs) 
                        GetWebs(siteCollection.AllWebs);  
                    } 
                    catch (Exception webE) 
                    { 
                        //You should log the error for reference 
                    } 
                    //reset the CatchAccessDeniedException property of the site 
                    //collection to true 
                    siteCollection.CatchAccessDeniedException = true; 
                }
            } 
        }
        public void GetWebs(SPWebCollection allWebs) 
        {
            //iterate through each site(web) 
            foreach (SPWeb web in allWebs) 
            {
                if(web.Exists)
                {
                    //For every site the program finds, the count will increase by one. If this code is executed by a user who does not have
                    //full control, then the count will be inaccurate as it will only return the number of sites the user can access.
                    test += 1;
                    DataTable table = web.GetUsageData(Microsoft.SharePoint.Administration.SPUsageReportType.browser, Microsoft.SharePoint.Administration.SPUsagePeriodType.lastMonth);
                    if (table == null)
                    {
                        HttpContext.Current.Response.Write("Null");
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("Not Null");
                    }
                }
            }
        }
        protected override void CreateChildControls()
        {
            BeginProcess();
        }
    }
}


Viewing all articles
Browse latest Browse all 25064

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>