CS Dev Guide: How to write a CSModule
First let’s make it clear what’s a CSModule? If you’re a .NET developer, you know that what HTTPModule is. CSModule is something similar to this which works for Community Server. By HTTPModule you could work on incoming requests and manipulate the response. Here is as same as there in smaller scale for this special application.
By CSModule you can govern on your response and add something to it or edit it. The best way to know more about CSModule functionalities is to take a look at some written CSModules. Some of them are here and I have some CSModules that you can find them in my files gallery.
But how we can write a CSModule? All you need is a good background in OOP and Community Server APIs. CSModule isn’t more than an implementation of an interface and adding a delegate to a handler. You can do this in a class library project. Anyway you need to have final class in a DLL to use your CSModule.
In CommunityServer.Components namespace, you have an interface which is named ICSModule. You must implement this interface to write a CSModule. To implement this you must implement your own Init() method. This method gets two parameters: An object of type CommunityServer.Components.CSApplication and an object of type System.Xml.XmlNode. Hence you need to import at least two namespaces to your class: System.Xml and CommunityServer.Components.
In your own Init() method, you must add a handler to one of available events in your CSApplication object. Currently CommunityServer supports several events to help you write good CSModules. Some of them are: PreRenderPost, PrePostUpdate, PreSearch, CSException and … I think names are self-explanatory and I don’t talk about them here. Use Intellisense to find full list.
You need to add a new handler to one of these events and this isn’t something more than adding a delegate to this event. For more information go to OOP in .NET! If you don’t have a good background in OOP, you’re not a developer!
Now you write your own method based on which you have added to this handler and develop it based on your needs. To do this you should use Community Server APIs and what is available for you in two passed objects as your parameters. They provide some common properties and methods for you to write a CSModule. Here I give the base structure of a CSModule:
VB:
Imports System.Xml
Imports CommunityServer.Components
Public Class MainModule
Implements CommunityServer.Components.ICSModule
Public Sub Init(ByVal csa As CSApplication, ByVal node As System.Xml.XmlNode) _
Implements ICSModule.Init
AddHandler csa.PreRenderPost, New CSPostEventHandler(AddressOf csa_PreRenderPost)
End Sub
Private Sub csa_PreRenderPost(ByVal Content As IContent, ByVal e As CSPostEventArgs)
' Put your logic here
End Sub
End Class
C#:
using System.Xml;
using CommunityServer.Components;
namespace CSModuleDemo_CSharp
{
/// <summary>
/// This is a sample for CSModule implementation
/// </summary>
public class MainModule : CommunityServer.Components.ICSModule
{
public void Init(CSApplication csa, XmlNode node)
{
csa.PreRenderPost += new CSPostEventHandler(csa_PreRenderPost);
}
private void csa_PreRenderPost(IContent Content, CSPostEventArgs e)
{
// Put your logic here
}// csa_PreRenderPost
}
}
Once you wrote your class and compiled it, you can go to your communityserver.config file and find the <CSModules> node to add this CSModule to your application and test it. To accomplish this task, you should add something similar to what I’ve added below:
<add name = "MyCSModule" type = "CSModuleDemo_CSharp.MainModule, CSModuleDemo_CSharp" />
That’s it! Writing a CSModule can be a good and sweet practice around Community Server code. After talking about some common APIs, I’ll write a sample CSModule and explain it here. But for now, you can take a look at my CSModules in file gallery. They are simple and can help you to find more about the process.
[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.
15 Comments : 05.07.06
Feedbacks
Thanks Keyvan for your article!

#1
Community Server Daily News
05.12.2006 @ 1:33 PM