FreeTextBoxWrapper for Community Server v2.1 & v2007 (Refresh)

It has been a while, since I have posted something about Community Server. Although I have received quite a lot requests, about refreshing FreeTextboxWrapper for CS v2.1 and CS v2007 controls, to the latest version of FreeTextBox (v6.2.2), in order to work correctly with newer browsers (Safari, Chrome, IE7 & IE8).

Well, here they are:

The new version of FreeTextBox is included and changes to JavaScript files to support the new FreeTextBox functionality.

 

(Last update 15 Jun 2009)

del.icio.us Tags: ,

Tagged with: ,
Posted in Community Server

SubSonic v3’s T4 Templates, Part 3

About a month ago, I created a first version of T4 templates for SubSonic v3 using T4Toolbox. They started as a fun way to learn using T4 transformation. Now, these templates are now intergraded in SubSonic v3 source.

If you are fan of SubSonic, or not, if you want to learn more about T4 transformation and how to use in a real life application, I suggest you download SubSonic v3 source and give it a try…

Happy transforming!

del.icio.us Tags: ,

Tagged with: ,
Posted in SubSonic

T4 templates and include files

It true that Microsoft provides no support for .tt template editing in Visual Studio 2008. They don’t plan to include this functionality in Visual Studio 2010 either – see feature request in Microsoft Connect. If Clarius Consulting hadn’t released their T4 Editor, there would be no support for editing T4 templates at all.

I have spend many hours lately in order to master T4 template creation. The most annoying thing about T4 templates, is that they work: TextTemplatingFileGenerator tool, the tool responsible transforming T4 templates to text artifacts, has no clue if a .tt file is a standalone template or an include template file. The tool transforms every file, without exception. When creating multi-file templates, this is really annoying – you get thousands of errors, which don’t really exist. When you are in the middle of template creation, this is really confusing.

How can you deal with this?

Actually there is only one solution: use .tt extension only for stand alone templates, or the starting template of multi-file template. Use an other extension, let’s say .ttinclude extension, for all other template files. TextTemplatingFileGenerator tool is not assigned as “Custom Tool” in the new extension, so transformation by default is disabled.

This looks nice, but .ttinclude extension, is not recognized as a known extension by Visual Studio or Clarius T4 Editor. You will get rid of the errors that are really annoying, but you loose all this nice functionality that Clarius T4 Editor provides. The good news is that you can deal with this, adding just two entries in your registry:


Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT.ttinclude]
@="VisualStudio.TextTemplating.1.2"

[HKEY_LOCAL_MACHINESOFTWAREMicrosoftVisualStudio9.0LanguagesFile Extensions.ttinclude]
@="{4217b9d4-cd01-4b28-bc12-e289a19ecc6d}"

The first one, provides a type for .ttinclude extension, and a icon to recognize the files. The second one, makes sure that Clarius T4 editor, recognizes .ttinclude extension as a T4 template, and provides T4 template editing.

Happy transforming!

del.icio.us Tags: ,
Posted in T4 Templates

SubSonic v3’s T4 Templates, Part 2

It’s only been two weeks I’ve playing around with T4 templates. Most of the time is spent in learning how to debug them, and how to schema databases using different database providers. I have just created the fourth version of SubSonic v3’s T4 templates. You can read a recent post, how to use them.

del.icio.us Tags: ,

Tagged with: ,
Posted in T4 Templates

T4 templates using own project .config file

One of the first problem one faces, as soon as he decides to include T4 templates in a project, is how to access .config file of the project. Most of the times .config files are used to store information as connection strings, as well as, other application wide settings, critical to the application. When a T4 template is transformed, it is not run under the context of the project it belongs; the context it runs is the context of the utility that performs the text transformation. Most of the times the utility is TextTemplatingFileGenerator.exe.

The need to access information from app.config from T4 templates, drive me create a T4 template include file, that gives me access to app.config information the same way as ConfigurationManagerobject. You can find this file here:

    I also included in the archive, a T4 template that demonstrate its usage; it’s easy to instantiate the object:

 IServiceProvider hostServiceProvider = (IServiceProvider)this.Host;  SettingsManager config = new SettingsManager(hostServiceProvider);
 


Then it’s easy to read the configuration from the project:

 
 this.ClearIndent();
 this.WriteLine("Namespace : {0}", config.Namespace);
 this.WriteLine("AssemblyName: {0}", config.AssemblyName);
 this.WriteLine("ProjectPath : {0}", config.ProjectPath);
 this.WriteLine("ConfigPath : {0}", config.ConfigPath);
 this.WriteLine("Is App : {0}", config.IsApplication);
 this.WriteLine("Is WebApp : {0}", config.IsWebApplication);

 // Display each ConnectionStringSettings.
 this.WriteLine("");
 this.WriteLine("ConnectionStrings:");
 this.PushIndent("\t");
 foreach (ConnectionStringSettings connectionstringentry in config.ConnectionStrings) {
   this.WriteLine("Name: {0}, ConnectionString: {1}, ProviderName: {2}", connectionstringentry.Name, connectionstringentry.ConnectionString, connectionstringentry.ProviderName);
 }
 this.PopIndent(); 
 this.WriteLine("");

 // Display each KeyValueConfigurationElement.
 this.WriteLine("ApplicationSettings:");
 this.PushIndent("\t");
 
 foreach (KeyValueConfigurationElement keyValueElement in config.AppSettings) {    this.WriteLine("Key: {0}, Value: {1}", keyValueElement.Key, keyValueElement.Value);
 }

 this.PopIndent();
 this.WriteLine("");

 

Hope you find the template useful…

del.icio.us Tags: ,
Tagged with: ,
Posted in T4 Templates

SubSonic v3’s T4 Templates

I short post in order to provide Subsonic v3’s T4 templates based on T4Toolbox, and a short demonstration video.

Starting with the video – download and play the video in full-screen:

The templates used for the video can be found here:

Finally some short notes about the templates:

  • When I saw the T4 templates that comes with the first SubSonic v3 Alpha, I must say I was scared! I really liked the smooth code generation method Subsonic v2 had. As soon as you drop the T4 templates in your project, error messages start popping from everywhere.
  • Before Christmas I found out T4Toolbox project in CodePlex. I knew nothing about T4 template code generation back then, but it looked like that was the way SubSonic v3 T4 templates should be implemented. Thanks to Oleg Sych weblog posts, and Clarius’ T4 Editor Community Edition, these templates were created. I only wish, I had the Professional version…
  • In order to use the templates in a project, first you have to make sure that your application has a app.config file with connection strings, and the Subsonic.Core.dll referenced. Then you can add the T4Toolbox folder and the SubSonic3 folder in your project. Thanks to T4Toolbox templates’ infrastructure, all the templates are implemented as classes and they don’t make code generation – there are no error produced and no popping error messages.
  • The code generation procedure is initiated from the last file added to the project – SubSonic3Creator.tt has all the information needed to make SubSonic’s classes generation successful. It holds the connection string’s name to the database, the namespace under the classes are generated, as well as the database name for SubSonic’s reference. All the files created are under this template – every time this template is saved, the code generation procedure is initiating.

    S3T4_1 S3T4_2 S3T4_3
  • You are free to use the templates in your projects – remember these SubSonic T4 templates are in early stage of developing and may contain errors. In order to avoid T4 template code generation errors you can remove the value, attribute Custom Tool is holding for each template in T4Toolbox and Subsonic3 folders. Only SubSonic3Creator.tt has to maintain the value in the attribute.
Tagged with: , ,
Posted in SubSonic

Community Server and Google AdSense

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.

I haven’t been a user of AdSense program, so I asked information about the program in order to implement the AdSense CSModule. I was under the impression, all I would have to implement was a BBCode tag that would include the user’s AdSense program code, in order to display ads and credit his account. Learning about the program, I found out, all the customization of the AdSense banner was done through JavaScript code. I was not happy about it, because it would be a potential exploit, as the CSModule would render code a user would provide, inside <script> tags.

In order to handle the JavaScript code inside AdSense tags, I used Community Server’s build-in functions that strip all the tags from string variables. In order to be sure, all I have left is secure code, I also removed all the lf/cr sequences and split the string buffer using the ";" character; this trick produces an array of JavaScript code lines. It was easy to decide which lines are safe, as I could "approve" lines for rendering, that will include names of the Google AdSense program variables.

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.

Posted in Community Server
Follow

Get every new post delivered to your Inbox.