Canarys | IT Services

Blogs

How to setup Module to Module communication in DotnetNuke

Share

Are you building modules in DotnetNuke?
Is one module depends on another module?
Want to exchange data between modules?

Here we can discuss how to set up Inter Module Communication in DotnetNuke. In DNN, with the help of IModuleCommunicator and IModuleListener interfaces we can setup the communication between the modules.

Here I would like to present all the required steps to setup the communication between modules. Before we start discuss on this, let’s understand who is communicator and who is listener.

Communicator, is a module which establish the communication with other module. In other words, a module which sends the information to other module.

Listener, is a module which gets the connection from other module. In other words, a module which receives the information from other module.

Module to Module Communication

Now I will practically show you how to configure the communication between DNN modules. In my example Module X will act as Communicator and Module Y will act as Listener.

Module X, in which I am showing all the available users from the current DNN portal in a DropDownList control. And when the user selected from the DropDownList, I am sending the selected user name to Module Y.

Module Y, in which simply I am receiving the selected user name from Module X and showing it in a Label control as message.

Communicator Module (Module X):

The UI page (view.ascx) of communicator module has a dropdownlist to populate all the user names from the portal:

<div>
<
asp:DropDownList ID=”ddlUsers” runat=”server” AutoPostBack=”true”      OnSelectedIndexChanged=”ddlUsers_SelectedIndexChanged”>
asp:DropDownList>
<
br />
<
asp:Label ID=”lblMessage” runat=”server” Text=””>asp:Label>
div>

In order to make this Module X as communicator module, in code behind (view.ascx.cs) page we need to implement the IModuleCommunicator interface. ModuleCommunicationEventArgs class would be used to share the information with other modules.

using DotNetNuke.Entities.Modules.Communications;

public partial class View : PortalModuleBase, IActionable, IModuleCommunicator
{

   public event ModuleCommunicationEventHandler ModuleCommunication

   private void Page_Load(object sender, System.EventArgs e)
{
      try
{
         if (!IsPostBack)
{
            ArrayList alUsers = UserController.GetUsers(this.PortalId);
ddlUsers.DataSource = alUsers;
ddlUsers.DataTextField = “DisplayName”;
ddlUsers.DataValueField = “UserId”;
ddlUsers.DataBind();
}
}
      catch (Exception exc)
{
            Exceptions.ProcessModuleLoadException(this, exc);
}
}

  protected void ddlUsers_SelectedIndexChanged(object sender, EventArgs e)
{
     if (ModuleCommunication != null)
{
        ModuleCommunicationEventArgs mcArgs = new        ModuleCommunicationEventArgs(this.GetType().ToString(), ddlUsers.SelectedItem.Text,    this.GetType().ToString(), “ModuleY”);
ModuleCommunication(this, mcArgs);
}
     else
{
lblMessage.Text = “Can’t communicate with other modules!”;
}
}
}

In the above code, selected user name is passing as parameter to ModuleCommunicationEventArgs class constructor and the object of it is passing a parameter to event handler.

Listener Module (Module Y):

The UI page (view.ascx) of Listener module has a simple UI with only one label control used to display the user name which is received from Communicator module (Module X).

<div>
<
asp:Label ID=”lblMessage” runat=”server” Text=””>asp:Label>
div>

In order to receive the information from communicator module, each listener module should implement IModuleListener interface which needs to implement OnModuleCommunication method. With the help of below code you can make the module as a listener module.

using DotNetNuke.Entities.Modules.Communications;

public partial class View : PortalModuleBase, IActionable, IModuleListener
{
  public void OnModuleCommunication(object s, ModuleCommunicationEventArgs e)
{
lblMessage.Text = “Received User Name: “ + e.Value;
}
}

When you call the module communication event handler on Module X, all the listener modules on the page will be activated and fires the OnModuleCommunication method in listener modules

You can make the communicator module as a listener module to other communicator module and also a communicator module can have multiple listener modules.

Limitation: You can setup the communication between modules only if the modules are located on the same page. It is not possible to make the communication between Module X which is on Page1.aspx and Module Y which is on Page2.aspx

Hope this helps..!

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.