Archive for August, 2009
Sharepoint (WSS/MOSS) Auditing VIEW On List Items
Recently I had a request from a client to log/audit when a user clicks on anything on a Sharepoint site. At first I thought, this will be easy, I will just turn on Auditing and select to log all VIEWs on all Items and then produce a report based on the Audit log. Well, things were not that simple.
It appears that Sharepoint logs VIEWs for Document Libraries very well, but fails miserably for logging VIEWs on every other type of Item, including Picture Libraries (this is true for WSS and MOSS). Basically, Sharepoint logs the List that the Item is contained in, but there is no logging as to the actual Item that was clicked on. So after Googling around bit I found that there was very little information on how to get around this and wanted to post a solution that works for most Items, but still fails for certain ones, e.g. Discussion Items for one. Maybe someone else here can enhance this solution to grab the missing ones.
My solution involves a custom HttpModule (not an HttpHandler), that inspects the IIS requests and if can load the Sharepoint context, Site, List and Item, then it manually adds the Audit Entry via the Sharepoint Object Model. This approach is only needed for non Document Libraries, since the regular Sharepoint Auditing does a better job at logging these types of Items.
The code for the HttpModule is below. You will need to do a bit of research on how to deploy an HttpModule, but there is plenty of information on this on the web. Essentially, you will need to compile this, strong name it, deploy it to the GAC and add an entry to the web.config of the Sharepoint site you wish to Audit.
Enjoy!
Imports System.Web
Imports Microsoft.SharePoint
Public Class AuditViewHttpModule
Implements IHttpModule
Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
End Sub
Public Sub Init(ByVal context As System.Web.HttpApplication) _
Implements System.Web.IHttpModule.Init
AddHandler context.PreRequestHandlerExecute, _
AddressOf Application_PreRequestHandlerExecute
End Sub
Private Sub Application_PreRequestHandlerExecute( _
ByVal _source As Object, ByVal e As EventArgs)
Dim _application As HttpApplication = Nothing
Dim _context As HttpContext = Nothing
Try
_application = CType(_source, HttpApplication)
_context = _application.Context
Dim _site As SPSite = SPContext.Current.Site
Dim _list As SPList = SPContext.Current.List
Dim _item As SPListItem = SPContext.Current.ListItem
If Not _site Is Nothing Then
If Not _list Is Nothing Then
If Not _item Is Nothing Then
If _
_list.BaseTemplate <> _
SPListTemplateType.DocumentLibrary _
Then
_list.Audit.WriteAuditEvent( _
SPAuditEventType.View, "", "")
End If
End If
End If
End If
Catch ex As Exception
' handle the error otherwise your
' sharepoint pages will crash!
End Try
End Sub
End Class