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...

Comments

  1. count s incrementing only once in a day, what to do to perform count everytime i click on url?

    ReplyDelete

Post a Comment

Popular posts from this blog

Hide Ribbon on SharePoint 2013 using CSS

Get Comment Count in SharePoint

Configure external site as content sources in sharepoint search