DotNetNuke Modules – Inter Module Communication

G03A0039 If you spend any time at all writing DotNetNuke modules, you will eventually run into a situation where you need one module to communicate with another.  There are several methods of achieving this communication, but there is one DotNetNuke-sanctioned way that looks a bit more confusing, at first glance, than it really is.  In fact, once you see how simple it is, you’ll wonder why you didn’t use it before.


Inter Module Communication (IMC) uses a basic event-based mechanism.  This means that you will have one module that sends the event and another module that receives the event.  Since the event is the same event, any module listening for the event will hear all of the events that fire on the page, not just the event you will fire.  Therefore, you’ll need to make sure that the event you fire has enough unique information in it that your listener can determine that it is an event it should pay attention to.

To implement IMC on your module you’ll need to add the following using statement to the top of both your listening module and the module that will be firing the event:

an style=”color: blue;”>using DotNetNuke.Entities.Modules.Communications;

This will allow us to add the appropriate interfaces to the module class.

To implement the interface that will allow us to fire the event, implement the IModuleCommunicator interface:

partial class ViewDMBSample :
    PortalModuleBase,
    IActionable,
    IModuleCommunicator
{

To implement the interface that will allow a module to listen for the event, implement the IModuleListener interface:

partial class ViewDMBSample :
    PortalModuleBase,
    IActionable,
    IModuleListener
{

To fire the event from your IModuleCommunicator module, you'll need to call ModuleCommunication(this, ModuleCommunicationEventArgs).

The first parameter will pass a pointer to the module object with the event.  You could use this to pull specific information from it using properties or methods you may have implemented on the module.  Keep in mind that if you want to use properties or methods from the module, it is best to implement an interface that you can apply to the module so that when the listener gets the object, you can cast the sender to the interface rather than trying to cast it to the ASCX object, which doesn't always work.

The second property is a DotNetNuke object that you will create and stuff with the following properties:

Property Description
Sender This is a string value that allows you to specify who sent the message.
Target This is a string value that allows you to specify who you want to pick up the message.
Text This is text that allows you to attach free form text to the payload.
Type Another string that you can use however you want, but it is best used to specify the type of the Value property.
Value An object that lets you pass whatever you want.

Once you have fired the message, the listener will need to pick it up.  You do this by implementing the OnModuleCommunication method.
<

span style="color: blue;">public void  OnModuleCommunication(
    object s, ModuleCommunicationEventArgs e)
{
    // Do something with the message here.
}

Other places talking about IMC:

DotNetNuke Inter-Module Communication or: How Your Modules Can Get ... - If you have been writing your own DotNetNuke modules for any amount of time you have probably run across the concept of Inter-Module Communication (IMC for short). IMC particularly refers to the facility that the DotNetNuke framework ...

Inter Module Communication in DNN using C# - Well, this post I will share with you my finding about the DNN IMC, something like you want to pass 1 or more values from one module to another module, imagine you created 2 modules, one is search module another one is result list ...

 

Other post in DotNetNuke - Module Development

Related Post

4 Responses to “DotNetNuke Modules – Inter Module Communication”

  • Tom:

    Nice post. Would you recommend using IMC over passing parameters through a query string to use one module as a kind of dashboard to control an other?

  • Dave:

    It would depend on what you were trying to do. IMC is used when you need to have one module on a page “talk” to another module on a page during a single round trip.

    You can’t do that with a query string because you’d need to have at least two round trips to the server. The first to figure out what the query string was suppose to be and the second to have the second module answer it.

  • Shey:

    Really enjoy this site with it’s straightforward, no-holds-barred approach to real-world development.

    Dave: “you can cast the sender to the interface rather than trying to cast it to the ASCX object, which doesn’t always work.”

    Please could you provide an example(s) of when this wouldn’t work, and why?

  • Dave:

    The problem is that sometimes the code will not compile even though it is correct. This can happen more often if you change the public methods and properties.

    There isn’t any code I can give you to replicate the problem because sometimes it will compile fine and other times it doesn’t.

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear