If like me you have worked on a mix of intranet, extranet and internet you will have no doubt been asked about capturing direct file clicks. So an example would be you wish to know the top PDF document that is being clicked on your public facing site. Out of the box SharePoint does its best with the usage analysis framework but does not capture everything that you may need. In this post I will show you how to use custom code to capture specific file type links and log them to a list. Firstly let's create the list to store the values, to do this I am going to create a content type and then associate this with a custom list.
Now have our new content type albeit a very basic one, we can then create our new list and associate accordingly.
So to make this work we could use some code that resides within the "Global.asax", I like this approach as it means you do not need Visual Studio to make changes and they can be done on the fly, however this is not the best approach. For this we will create a custom "HttpModule" that we will register within the "web.config". So to begin let's create a class library project in Visual Studio and ensure it inherits from the "IHttpModule" class. I am going to name my class "CountDirectLinks.cs" and then create a utility class for any functions etc that I need and call that "CountDirectLinksUtilities.cs".
So firstly ensure that we are inheriting correctly and have the correct namespaces for this to work.
We now need to create the base methods for capturing the relevant events. To do this we will use the following code:
This initialize method attaches to the current context and calls the "AuthenticateRequest" method. This allows us to intercept anything that happens after a user has been authenticated. We could use other methods if needed such as "BeginRequest"; this would capture anything before it was authenticated. In our case we wish to get the clicked links when someone has been authenticated. Our code should look like the following:
Now part of this solution was to allow the Administrator to specify the file types that they wish to capture and log. To do this we will simply create the following "appSettings" within the web.config. We will also add two extra ones for the root site URL and the list name that we are to log to. This could be done differently but I chose this for this example.
The list of file types will simply be a separate list that we will then read into the HttpModule and use accordingly. So firstly let's create our code for inserting the content into our list. We need to create the method and then get the values from the "AppSettings" within the "web.config" using the "ConfigurationManager" namespace.
We then add the following code to get a SPSite and SPWeb object.
Now we can add our code that will create the new list item.
Notice that I am calling a utility method called "GetFileName" this is because when I grab the URL I get something like this:
http://intranet/Dev/Shared%20Documents/myDocument.pdf
I would need to parse this out so I would get just the name of the file instead. Even though this would work I felt it would be better to call a function that would give me access to the metadata fields that were associated with the actual file. This would then allow me to extend the solution further by getting other details about the file rather than just the URL. For this example I get the actual name of the file as shown in the library or list.
This method is a re-hash of the one I used in my Search API posts a while back. The code should be similar to this:
So now we have our method created that will actually insert to our logging table, now let's modify the "AuthenticateRequest" method so it reads the file types and then inserts the log as needed.
As you can see this grabs the current "HttpApplication" which comes from the initialize event, grabs the file types as an array and parses them and inserts the log when needed. Okay so now we have built it, let's built and deploy. In my case it creates a DLL, which I add to the GAC (development box), and then add the following entry to the "HttpModules" section of the "web.config" file.
So test this we can upload some files into a library and then access them. The following screenshots is the process I took:
If we now look in the custom logging list we can now see two entries for the viewed files:
If you remember we are only logging PDF, PNG and DOCX file extension but this could be changed.
As you can see with a little customization we could capture all kinds of content about the current user or visitor accessing the site. In the next post we will extend it to capture more information and events through the system. J