Wednesday, 7 September 2011

SPS.Usage.Visits or How to get the number of visits of a Site from a Web Part in Sharepoint 2010

I am sure you have ask yourself how to monitor the number of visits that a particular site collection has and from there improve it or leave it as it is. Until now we did not have any mechanism to test that but Microsoft incorporate the property Visits in the SPSite object to allow us to get the results.

I am not going to do a Step by Step project of how to build a Web Part that display the visits because I think that if you are reading this you are mature enough to develop your own Web Part and implement these lines of code.

The following code is really basic, it will put the number of visits of a Sharepoint site collection (where the Web part lives) in a string:

private void ElevatedSub()
   SPSite _sSite = new SPSite(SPContext.Current.Site.ID);
   string _sNumberOfVisits = _sSite.Usage.Visits.ToString();


Anonymous said...

Why can't you get the site via SPContext.Current.Site and not instantiate an object?

Anonymous said...