Count this page Hits


Hi,

Sometimes we require some webpart to calculate the number of hits on a particular page. I am sharing a Hit counter webpart which will count the number of hits on any page if added to any particular page.

Following are the steps to create this Webpart:

Step1: Create a SharePoint 2010 solution using default basic template.

Step2: Add a Simple webpart named “Hit Counter” to the project. The webpart class will contains following code:
using System;
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.WebControls;
namespace HitCounterWebPart.HitCounterWebPart
{
[ToolboxItemAttribute(false)]
public class HitCounterWebPart : WebPart
{
string cLoginName = string.Empty;
string cDate = string.Empty;
string cURL = string.Empty;
SPList myList;
private const string listName = "HitsList";
private const string fieldUrl = "Url";
private const string fieldDate = "Date";
private const string fieldUser = "UserName";
protected override void Render(HtmlTextWriter writer)
{
SPWeb myWeb = SPContext.Current.Web;
string cMonth = DateTime.Today.Month.ToString();
string cDay = DateTime.Today.Day.ToString();
string cYear = DateTime.Today.Year.ToString();
cLoginName = myWeb.CurrentUser.LoginName;
cDate = cMonth + "/" + cDay + "/" + cYear;
cURL = HttpContext.Current.Request.Url.AbsoluteUri;
if (cLoginName != "" && cDate != "" && cURL != "")
{
myList = myWeb.Lists[listName];
SPQuery myQuery = new SPQuery();
myQuery.Query = " <OrderBy>" +
"<FieldRef Name='ID' />" +
"<FieldRef Name=" + fieldUrl + " />" +
"<FieldRef Name=" + fieldUser + " />" +
"<FieldRef Name=" + fieldDate + " /></OrderBy><Where><And><And><Eq>" +
"<FieldRef Name=" + fieldUrl + " />" +
"<Value Type='Note'>" + cURL + "</Value></Eq>" +
"<Eq><FieldRef Name=" + fieldUser + " />" +
"<Value Type='Text'>" + cLoginName + "</Value></Eq></And>" +
"<Eq><FieldRef Name=" + fieldDate + " /><Value Type='Text'>" + cDate + "</Value>" +
"</Eq></And></Where>";
SPListItemCollection myItemcol = myList.GetItems(myQuery);
if (myItemcol.Count > 0)
{
writer.Write(myList.ItemCount.ToString());
return;
}
else
{
SPListItem myItem = myList.Items.Add();
myItem[fieldUrl] = cURL;
myItem[fieldDate] = cDate;
myItem[fieldUser] = cLoginName;
myWeb.AllowUnsafeUpdates = true;
myItem.Update();
myWeb.AllowUnsafeUpdates = false;
writer.Write(myList.ItemCount.ToString());
}
}
}
}
}

Step3: Create a feature event reciever. paste the code below:

using System;
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.WebControls;
namespace HitCounterWebPart.HitCounterWebPart
{
[ToolboxItemAttribute(false)]
public class HitCounterWebPart : WebPart
{
string cLoginName = string.Empty;
string cDate = string.Empty;
string cURL = string.Empty;
SPList myList;
private const string listName = "HitsList";
private const string fieldUrl = "Url";
private const string fieldDate = "Date";
private const string fieldUser = "UserName";
protected override void Render(HtmlTextWriter writer)
{
SPWeb myWeb = SPContext.Current.Web;
string cMonth = DateTime.Today.Month.ToString();
string cDay = DateTime.Today.Day.ToString();
string cYear = DateTime.Today.Year.ToString();
cLoginName = myWeb.CurrentUser.LoginName;
cDate = cMonth + "/" + cDay + "/" + cYear;
cURL = HttpContext.Current.Request.Url.AbsoluteUri;
if (cLoginName != "" && cDate != "" && cURL != "")
{
myList = myWeb.Lists[listName];
SPQuery myQuery = new SPQuery();
myQuery.Query = " " +
"" +
"" +
"" +
"" +
"" +
"" + cURL + "" +
"" +
"" + cLoginName + "" +
"" + cDate + "" +
"";
SPListItemCollection myItemcol = myList.GetItems(myQuery);
if (myItemcol.Count > 0)
{
writer.Write(myList.ItemCount.ToString());
return;
}
else
{
SPListItem myItem = myList.Items.Add();
myItem[fieldUrl] = cURL;
myItem[fieldDate] = cDate;
myItem[fieldUser] = cLoginName;
myWeb.AllowUnsafeUpdates = true;
myItem.Update();
myWeb.AllowUnsafeUpdates = false;
writer.Write(myList.ItemCount.ToString());
}
}
}
}
}

This will create a new list will be used to store the URL and their hitcounts. These are only the code files required to create the webpart. Now solution is ready for deployment.

After successfully deployment of this solution, add the hit counter web part to the page.

Thanks for time…

Advertisements

One thought on “Count this page Hits

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s