For a while I have been asking about Scott’s ShareIt Module and wether the system will run inside the Community Server 2007 system, there have been several hints that a new release will soon be coming out. Now with a lot of sites starting to pick up the share it type links and icons on there systems (Link www.digg.com, www.dotnetkicks.com and others) I decided that I needed to throw the same sort of system on my site.
With Scott’s Module not having a CS 2007 Release I thought that I would look into the code and try and get it to work. The good thing is that if you open the source project attach the new community server libraries.... it all works fine. So in the end I just recompiled the Modules under 2007 and off I went.
Digging deeper I found that the ShareIt system that Scott had was good, but I wanted more, I wanted to have the nice little graphic at the bottom. With Scott’s code as a base as well as some other sources I found I started to code my own DiggIt Module, after about 20 minutes of work and some testing I now have a DiggIt module that will add the code to the bottom of each post. The mod also allows the user to click on the graphic and submit the Digg article, at the same time populating some of the fields during the submit.
Anyway here is the code and process that I took to create the module...
Creating the DiggItModule
The first step is to create you base assembly; I have called mine 'VirtualRealm.CS.Modules' as I plan on writing more. When the project has finished setting it's self up add the following Community Server Library references to the project references.
CommunityServer.Blogs
CommunityServer.Components
Next rename the Class.cs file to 'DiggItModule.cs' and add the following using statements to the top of the class file.
[code language="C#"]
using System.Xml;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
[/code]
Change the main definition of the class so it looks like this.
[code language="C#"]
namespace VirtualRealm.CS.Modules
{
public class DiggItModule : ICSModule
{
}
}
[/code]
Add the following code to the class, this will initialize the module and set up the event handler that we will be using. If you are going to be looking into creating more modules you should have a look at the references on the Communityserver.org site for the other types of events that you can use.
[code language="C#"]
public void Init(CSApplication csa, XmlNode node)
{
csa.PreRenderPost += new CSPostEventHandler(csa_PreRenderPost);
}
private void csa_PreRenderPost(IContent content, CSPostEventArgs e)
{
}
[/code]
Next we add the variables that we will be using in the code, just under the init procedure add the following.
[code language="C#"]
private bool _syndicate = true;
private bool _web = true;
private DateTime _dateFilter = DateTime.MinValue;
[/code]
The last stage we need to do is to add the following code section inside the event handler.
[code language="C#"]
if (e.Target == PostTarget.Web)
{
if (!_web)
return;
}
else if (e.Target == PostTarget.Syndication)
{
if (!_syndicate)
return;
}
if (e.ApplicationType == ApplicationType.Weblog)
{
WeblogPost wp = content as WeblogPost;
bool includePost = (e.Target == PostTarget.Web || wp.PostDate >= _dateFilter);
if (wp != null && wp.PostLevel == 1 && includePost)
{
string link = Globals.FullPath(BlogUrls.Instance().Post(wp));
string title = wp.Subject;
string titleUrlEncode = Globals.UrlEncode(wp.Subject);
wp.FormattedBody =
string.Format("{0}\n<script type=\"text/javascript\">digg_url = '{1}';digg_title = '{2}';</script><script src=\"http://digg.com/tools/diggthis.js\" type=\"text/javascript\"></script>",
wp.FormattedBody, link, title);
}
}
[/code]
With all the coding done you should be able to compile the project, for the first time to check that it works compile the code under debug, but remember that when you publish the code and ship it to your web site that you compile it under a release mode.
Once compiled and working to install the module you will need to upload the dll to the bin directory of you web site. Next open your communityserver.config file and add the following reference to the csmodules section. I added mine last in the list.
[code language="C#"]
<add name="DiggIt" type ="VirtualRealm.CS.Modules.DiggItModule, VirtualRealm.CS.Modules" />
[/code]
The last thing is that you should touch you web.config file to make sure that the site recompiles and relinks every thing. Now when you go to your blogs pages you should see the DiggIt graphic and links on the bottom of each post.
This is only my first CSModule and I was surprised at how easy it was to put one together. I admit that a lot of the code in the mod is from Scott’s ShareIt Module, but it helped a lot and could be used as a base for other modules of the same type. I am presently putting together a KickItModule which I will release and post. For more information on CSModules and some templates that you can use you should check out the Community Server Documentation Pages as well as the Community Server MVPs blogs as they all hold valuable information on how to do lots of different tasks with CSModules. You could also do what I have done and look at some of the open source modules that have already been written, as you might find that the task you want to do may be simular to one of those.