Keyvan Nayyeri

God breathing through me

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.

15 Comments

Community Server Daily News
May 12, 2006 1:33 PM
#
News Section fluidity continues.&nbsp; Today we're adding "Mods and Add-ins" to highlight...

The Original .NET Geek
Jun 04, 2006 1:00 PM
#
Community Server Rocks!&nbsp; Since I have installed my CS site, I have been working to modify it.&nbsp;...

Simone
Jun 21, 2006 9:47 AM
#
But what is the difference between a Module and an AddOn? Simone

Keyvan Nayyeri
Jun 21, 2006 11:55 PM
#
An Add-on can be a new custom control or something like Mail Gateway but CSModule is something like HTTPModule that will be fired when some pre-defined events occur.

Simone
Jun 28, 2006 6:45 AM
#
Which do you think is the best way to develop and debug modules? Have the SDK build and run, and then develop the CSModule from that solution? Or better to create a complete new solution and attach to the ASP.NET process to debug the new module?

Keyvan Nayyeri
Jun 28, 2006 7:04 AM
#
I personally create a new separate Class Library Project, add DLLs to it and develop my CSModule there. But this is my personal method because I'm completely familiar with APIs and don't get in trouble with bugs/exceptions/deployment and don’t recommend it to all other developers. A good way is to add a new Class Library project as a new reference to CS SDK, write your code then just configure your communityserver.config file. don’t forget that in this case you’ll need CS SDK which isn’t necessary to develop CSModules and work on CS APIs. I wrote some of my MODs when CS was in Beta and I couldn’t have access to source code and my first approach was sufficient to reach to my aims.

Simone
Jun 28, 2006 2:30 PM
#
Thank you Keyvan, so u suggest to create a new solution, add references to the needed assemblies and create the CSModule. Then add it to the communityserver.config file, and attach the debugger to the worker process. Sounds good to me

rape videos
Jul 05, 2006 7:14 AM
#
Your article is quite right, thanks.

TrackBack
Jul 08, 2006 12:55 AM
#
Last weekend I said that I would probably not be able to keep up with posting each day, well I lied ...
The CS MVPs have been crazy busy lately, nose to the grindestone pounding out some CSModules that will

Richard
Aug 21, 2006 7:40 PM
#
The full list of events you can bind to from community server 2.0: EventAuthorizePost EventPrePostUpdate EventPreProcessPost EventPostPostUpdate EventPrePostAttachmentUpdate EventPostPostAttachmentUpdate EventPreRenderPost EventPreUserUpdate EventPostUserUpdate EventUserRemove EventUserKnown EventUserValidated EventPreSectionUpdate EventPostSectionUpdate EventPreSectionGroupUpdate EventPostSectionGroupUpdate EventRate EventFavorite EventUnhandledException PreEventSearch PostEventSearch

J-O Eriksson's blog
Oct 30, 2006 11:48 AM
#
Whether you are a developer, or a Community Server admin that haven't got much experience of .NET Development,

Alpha's Blog
Nov 10, 2006 3:44 AM
#
[http://joeriksson.com/archive/2006/10/30/The-Secrets-of-CSModules.aspx] Whether you are a developer,

Scott Gettis
Mar 21, 2007 5:43 PM
#
I am reading you new CS book and I am trying to implement the FeedCopyRight Module in Chapter 13; however it is not working. I am pasting my code below. Is there a way to debug a csmodule? Also is there a way to see if it is running? I have tried to implement a CSModule before but I could not get it to work. using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.IO; using CommunityServer; using CommunityServer.Components; namespace Evanta { public class FeedCopyright: ICSModule { private string filePath; public void Init(CSApplication csa, XmlNode node) { CSContext context = CSContext.Current; csa.PreRenderPost += new CSPostEventHandler(csa_PreRenderPost); XmlAttribute filePathNode = node.Attributes["filePath"]; try { this.filePath = context.MapPath(filePathNode.Value); } catch { throw new CSException(CSExceptionType.UnknownError, "Enter a relative address for your file"); } } private void csa_PreRenderPost(IContent Content, CSPostEventArgs e) { if ((e.ApplicationType == ApplicationType.Weblog) && (e.Target == PostTarget.Syndication)) { Content.FormattedBody += getCopyright(); } } private string getCopyright() { StreamReader Reader = new StreamReader(File.Open(this.filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); string copyright = Reader.ReadToEnd(); Reader.Close(); return copyright; } } }

slnavn2000
Sep 09, 2008 9:57 PM
#

Thanks Keyvan for your article!

Leave a Comment





Ads Powered by Lake Quincy Media Network