Not many programs have managed to amaze me with their capabilities. Certainly Community Server is one. It is really a nice feeling to know, you can implement what you need on the platform you use.
It all started when a site member asked me to provide the ability to host Google AdSense in his blog. The first thing came to my mind was to create new weblog skins in order to implement AdSense banner zones, but as more webloggers were sharing the same skins, it would only confuse them. Part of the service would be to allow each member to customize his ads zone; so except weblog skin creation, I would have to implement new pages for the control panel too. Not quite easy…
Then I came up with the idea to implement a CSModule that would allow the display of an AdSense banner in weblogs posts. This idea did not seem wise at the beginning; the users would be able to use the CSModule all over the CS site and flood all posts with ads. Well, I could easy control the use of CSModule by requiring each member to belong to a certain security role I would assign.
While I was testing the new developed CSModule, I discovered that, an authorized member could include AdSense tag in a forum’s post. It was not the way I wanted CSModule to be used. The CSModule mechanism provides a property that stores the type of CS application is executing CSModule each time, and I had to limit the use of CSModule to render the AdSense code only for the Weblog application.
Completing this task, I deployed the assembly to the production server, and provided site members with instructions how to use the AdSense CSModule in their blogs. It was no more than two hours later, I got a message from a member, that he could not use the CSModule, because he would have his AdSense account suspended! There is a policy in Google AdSense program; a user can not display more than 3 AdSense banners in a webpage. I am not sure, if I had read this policy or not, but probably I just ignored it, as it would not help me to implement the CSModule. For sure, the CSModule was working, it was doing was it was designed to do. On each weblog page that renders a list of posts, the CSModule was rendering an AdSense banner for each post; that was definitely a violation of AdSense policy.
I must admit, I thought I was completely busted that moment! I had doubts when I decided not to implement new skins for the weblogs and just go with the CSModule implementation. I thought my decision was just wrong. But again, one of my favorite features on Community Server, url re-writing, gave me a push to explore the problem a little more. Community Server is re-writing the url of each request; it has mechanisms that parse each request url, split the address part from query data, and transform them into a new url, using the data in siteurls.config. So, if I could find a way to get the name of the template the url re-writing mechanism was using to transform the request url, I could figure out in which page the CSModule was running and make a decision, to render or not, the AdSense banner.
Half of the problem was to find the names of the templates I wanted the AdSense banner to be rendered. These were the pages that a weblog post would appear by its self. The second half of the implementation was to figure out which page had requested CSModule to render. My intention was to check the code Community Server was using to parse the requests, find some functions and use them with the request url, I would get through from CSContext object.
I must admit, I spend about half an hour checking code, without having a solution for my task; I was not checking for the obvious: Most of the times all the information processed are stored inside the CSContext object that is created with each request. Why would not the CSContext object include the name of the transformation template? I suppose that something flashed over my head that moment!
You probably guessed the answer; I was correct. There is a property in CSContext object that stores the name of the template! As for the implementation, I had to check if the name of the transformation template for the current url existed in the list of the templates I had already found.
Using AdSense CSModule is easy, but there are several lines of code the tag requires each time in order to render the AdSense banner. By creating a TextPart that holds the AdSense tag code, and using the TextPart’s tag, adding an AdSense banner to each weblog post is fast!
I would post the AdSense CSModule assembly now, but I am packing for Barcelona, to attend TechEd Developers 2007. I have little time to catch the plane to Barcelona and no time to create a package for the AdSense CSModule assembly. If you would like to see the CSModule in action, check this blog (it is Greek, so you probably won’t be able to read it, but you follow the links for each post, and see the AdSense banner on the bottom of each post). I suppose by this time next week, I will have the AdSense CSModule available for everyone to download.