Products and Services
Graffiti CMS
Learn more about our simple content publishing platform.
Harvest Reporting Server
Get business intelligence tools for measuring online behavior.
Professional Services
Consulting, creative, and Web services from the people who know Community Server best.
Solutions
Telligent
Learn more about our team at Telligent.com.
 

Writing and Configuring a CSModule

To write CSModules you must implement an interface called ICSModule.The ICSModule is defined in the CommunityServer.Components.dll. You must add a reference to this .dll before you can write any CSModules.

You should also add references to any application-specific .dlls you will need.

public interface ICSModule
{
    void Init(CSApplication csa, XmlNode node);
}

The interface has just one method to implement, Init with two parameters:

  • CSApplication - Provides you with a list of events you can wire up.
  • XmlNode - Provides you with the XMLNode used to configure the Module. You can ignore this unless you add some custom configuration options.

After you create and compile your, use the following steps to deploy it:

  • Copy your new .dll to your web sites bin directory.
  • Open up your CommunityServer.config file, find the CSModule element and add a new element which looks like this:

<add name="Your_Module_Name" type = "Namespace.ClassName, Assembly" />

Community Server does not throw a public exception if a module fails to load. You will need to check your Community Server Event log to see if there are any problems. As long as the log does not mention your module by name or type, you are in the clear.

A couple of other points:

  • Most CSModules use a custom delegate signature. This includes both a strongly typed access to the object you are working on as well as custom EventArgs. In most cases, you can use these custom EventArgs to figure out some details about your object, such as its state (Creating, Updating, or Deleting).
  • The strongly typed object references are to the Community Server base objects such as Post. In many cases working with the base objects is all right. If you ever need to work with an application specific instance, you will need to case it first: WeblogPost wp = post as WeblogPost.

Example: Adding after user creates new account

Here is a sample CSModule:

using System.Xml;
using CommunityServer.Components;
namespace Telligent.Samples
{
    /// <summary>
    /// Sample CSModule which can be used to invoke an action after a new user registers an account
    /// </summary>
    public class UserCreateModule : ICSModule
    {
        public UserCreateModule()
        {
        }
        public void Init(CSApplication csa, XmlNode node)
        {
            //wire up an event which is fired after the user change has been committed to the database
            csa.PostUserUpdate += new CSUserEventHandler(csa_PostUserUpdate);
        }
        private void csa_PostUserUpdate(User user, CSEventArgs e)
        {
            //we only care about new users, so let's filter for it
            if (e.State == ObjectState.Create)
            {
                //now we can do something such as assign roles, send email, etc.
                //Send Email, etc.
                //NOTE: If you want to make a change to a user value (or profile) you need an editable user object
                User editableUser = Users.GetUserWithWriteableProfile(user.UserID,null,true);
                //Do Something
                Users.UpdateUser(editableUser);
                */
            }
        }
    }
}

In the above code you complete the following tasks:

  1. Implement the ICSModule interface.
  2. Wire up a handler on the PostUserUpdate event. This event will fire after any user record is updated in the database.
  3. In the handler method, check to see if the State of the object requests ObjectState.Create. This tells us that this user instance we just now added to the datastore. On all subsequent updates (even the one the in example, the value will be ObjectState.Update).
  4. Implement the custom logic. You can send an email, update the users record with data from another source such as Active Directory, and so on.

To finish off this module, you only need to add the following line to the CSModules section in the CommunityServer.config file:

<add name = "UserSample" type = "Telligent.Samples.UserCreateModule, Telligent.Samples" />

Note: This assumes the assembly name is Telligent.Samples.dll.