DotNetNuke 4 C-sharp Modules

DotNetNuke 4 is written for ASP.NET 2.0 using Visual Basic. I’m not a big fan of VB, so I do all my .NET development in C#. Luckily, Microsoft has made these languages play very well together, so you can write C# classes that extend classes in assemblies compiled from VB. However the DotNetNuke guys don’t have a lot of documentation about using C#. Their tutorials for writing modules are also very “in-the-box” that assume you want to write modules that work in a very specific way, especially when it comes to the database.

Their module development is very structured, including an MVC-type of framework that feels very forced. They have good ideas about the number of abstraction layers in the data access, but it seems far too complex for, say, a simple guestbook module, and the process assumes that you are creating a new kind of object that will require persistance. It also requires you to slop code all over your DNN installation, where I wanted to package a up the module a little more cleanly. So I started thinking, “what about the Hello World?” So I wrote one in C#.

  1. Create a new empty project in VS, and add a reference to DotNetNuke.dll
  2. Add a new web control called HelloWorld.ascx, with a codebehind file of HelloWorld.ascx.cs.
  3. Instead of extending System.Web.UI.UserControl like you normally would, extend DotNetNuke.Entities.Modules.PortalModuleBase.
  4. Add a label control with a Message to display. The contents of my files are below:
  5. HelloWorld.ascx:
    [html]

    [/html]

    HelloWorld.ascx.cs:
    [csharp]
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Web.UI.WebControls;

    namespace BrianSamson.DNN.Modules.HelloWorld
    {
    public class HelloWorld : DotNetNuke.Entities.Modules.PortalModuleBase
    {
    public Label message;

    protected void Page_Load(object sender, EventArgs e)
    {
    message.Text = “Hello, World!”;
    }

    protected override void OnInit(EventArgs e)
    {
    this.Load += new System.EventHandler(Page_Load);
    }
    }
    }
    [/csharp]

  6. You’ll also need a stub Controller. This is where you can implement the searchable/portable interfaces if you want. I chose not to so there is not a lot to this class.
  7. Controller.cs:
    [csharp]
    using System;
    using System.Collections.Generic;
    using System.Text;
    using DotNetNuke;

    namespace Trimble.Outdoors.DNN.Modules.TripView
    {
    public class Controller : DotNetNuke.Entities.Modules.PortalModuleBase
    {

    }
    }
    [/csharp]

  8. To deply as a module, you need a to make a file called “HelloWorld.dnn”. It’s an XML configuration file that should look like this:
  9. [xml]

    Hello World
    HelloWorld
    HelloWorld
    HelloWorld
    HelloWorld by BrianSamson.com
    01.00.00
    BrianSamson.DNN.Modules.HelloWorld.Controller

    Hello_World
    0

    DesktopModules/HelloWorld/HelloWorld.ascx
    View

    HelloWorld.ascx

    HelloWorld.dll

    [/xml]

  10. That’s pretty much it. Make a zip file that has HelloWorld.ascx, HelloWorld.dll, and HelloWorld.dnn. Upload that file to DNN, and run the channel.

Hello World Sceenshot

I like this method because it allows you the freedom to develop however you want. In my case, I have an external database with NHibernate objects as accessors, so rewriting a DAO doesn’t make a lot of sense. And maybe it’s because I come from a java background and am just not inclined to do things the “dot net way.”

Here is the a Zip file with the source code, in case you don’t feel like copy pasteing. You will need to have your own copy of DotNetNuke.dll, which you can get from DotNetNuke.com by downloading the starter kit:
Hello World Source Code



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.