Friday, January 1, 2010

Hit Count Statistics with Monthly Usage and Site Owner List

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Xml;
using System.Data.SqlClient;

public partial class SiteDetailsOwner : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SPSite rootSite = SPContext.Current.Site;
SPWebApplication webApp = rootSite.WebApplication;
//SPWebApplicationCollection webApplicationCol = SPWebService.ContentService.WebApplications;
DataTable tbl = new DataTable();
tbl.Columns.Add("Id", typeof(string));
tbl.Columns.Add("Title", typeof(string));
tbl.Columns.Add("Url", typeof(string));
tbl.Columns.Add("RelativeUrl", typeof(string));
//tbl.Columns.Add("ContentDB", typeof(string));
//tbl.Columns.Add("NoSubSites", typeof(string));
//tbl.Columns.Add("Size", typeof(string));
//tbl.Columns.Add("Bandwidth", typeof(string));
//tbl.Columns.Add("Hits", typeof(string));
//tbl.Columns.Add("Visits", typeof(string));
tbl.Columns.Add("MonthlyUsage", typeof(string));
tbl.Columns.Add("NoUsersinRoot", typeof(string));
tbl.Columns.Add("OwnerGrpUser", typeof(string));
DataRow row;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
//commented
// foreach (SPWebApplication webApp in webApplicationCol)
{ //Web application name
for (int i = 0; i < finaltotal =" 0;" site =" webApp.Sites[i];" j =" 0;" web =" site.AllWebs[j];" sites =" web.Webs;" itot =" 0;" row =" tbl.Rows.Add();" itot =" PopulateUserUsageData(subSite);" autogeneratecolumns =" false;" visible =" true;" datasource =" tbl;" pageindex =" 0;" pageindex =" e.NewPageIndex;">
/// //Users who visited the site
///
///
public int PopulateUserUsageData(SPWeb site)
{
int totcount = 0;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
DataTable Dt = new DataTable();
//Users who visited the site
Dt = site.GetUsageData(SPUsageReportType.user, SPUsagePeriodType.lastMonth);
totcount = 0;
if (Dt != null)
{
foreach (DataRow r in Dt.Rows) // Loop over the items.
{
int itemCount = 0;
itemCount = Convert.ToInt32(r["Total Hits"]);
totcount += itemCount;
}
}
});
return totcount;
}
public String SelectData(SPWeb sWeb)
{
string sName = null;
string sAllUsers = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
foreach (SPGroup Grp in sWeb.Groups)
{
if (Grp.Name == Grp.Owner.ToString())
{
foreach (SPUser singleUser in Grp.Users)
{
sName += singleUser.Name + ",";

}
sAllUsers += sName;
string[] words = sAllUsers.Split(',');
List list = new List();
foreach (string word in words)
{
if (!list.Contains(word))
{
list.Add(word);
}
}
string sspl = string.Join("," + Environment.NewLine, list.ToArray());
//sspl.Remove(sspl.Length - 1, 1);
sspl = sspl.Substring(0, sspl.Length - 3);
sAllUsers = sspl;
}
}
});
return sAllUsers;
}
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
SPWeb Web = SPContext.Current.Web;
string strUrl =
Web.ServerRelativeUrl + "/_catalogs/masterpage/default.master";
this.MasterPageFile = strUrl;
}
}
======================SiteDetailsOwner.aspx=======================
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/_layouts/application.master" CodeFile="SiteDetailsOwner.aspx.cs" Inherits="SiteDetailsOwner" %>
<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages,
Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint"
Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint, Version=12.0.0.0,
Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities"
Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0,
Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


AutoGenerateColumns="False" BorderWidth="2" GridLines="Both"
AllowPaging="false"
AlternatingRowStyle-BorderStyle="Solid"
CssClass="mGrid"
PagerStyle-CssClass="pgr" Width="98%"
AlternatingRowStyle-CssClass="alt" OnPageIndexChanging="gdView_PageIndexChanging" >





ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
Site Hit Count Statistics with Monthly Usage

No comments:

Post a Comment