DotNetNuke Complete Guide to Inter Module Communication (IMC) using VB code behind

To set up the Inter Module Communication (IMC) functionality, there are two pieces required, a listener and communicator.  I will describe the process by first setting up the communicator, in module X, then setting up the listener, in module Y.

 

Since most of our projects require minimal postbacks, I will also set this up to use ajax.  While DNN has a feature “supports partial rendering,” often times it causes conflicts with IMC

 

Set up the Communicator

 

  1. Create Module X using the Christoc DotNetNuke 7 VB.Net DAL2 Compiled Module template

  2. In View.ascx add the dropdown box that will be populated with the DNN users

 

View.ascx:

 

<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>

 

  1. Open View.ascx.vb and add the necessary namespaces to the “Imports section” (see code below)

  2. Under the class declaration, Implement the IModuleCommunicator interface

  3. Setup the dropdown box to be populated upon page load

  4. create the ddlUsers_SelectedIndexChanged protected sub

  5. Create a new instance of ModuleCommunicationEventArgs

  6. Set up the ModuleCommunication arguments

 

View.ascx.vb:

 

'''''''''''''''''''''''''''''''''''''''''''''''''''''

' Project Description: Module for IMC Test'     

' Author: Kalen Shuck

' Date: 6/12/15

'''''''''''''''''''''''''''''''''''''''''''''''''''''

Imports DotNetNuke

Imports DotNetNuke.Entities.Modules.Actions

Imports DotNetNuke.Entities.Modules

Imports DotNetNuke.Services.Exceptions

Imports DotNetNuke.Services.Localization

Imports ServiTech.Modules.IMCTest_X.Components

Imports DotNetNuke.UI.Utilities

Imports System.Web.UI

' Import namespaces for classes we will use later.

' The following sets allows access for the IModuleCommunicator Interface.

Imports DotNetNuke.Entities.Modules.Communications

' The following allows DNN to populate the dropdown with the list of users.

Imports DotNetNuke.Entities.Users

 

Partial Public Class View

   Inherits PortalModuleBase

   Implements IActionable

   ' Implement the IModuleCommunicator interface.

   Implements IModuleCommunicator

   ' Setup the ModuleCommunication Event.

   Public Event ModuleCommunication(ByVal sender As Object, _

       ByVal e As ModuleCommunicationEventArgs) _

       Implements IModuleCommunicator.ModuleCommunication

   ''' <summary>

   ''' Populate the dropdown list on page load.

   ''' </summary>

   ''' <param name="sender"></param>

   ''' <param name="e"></param>

   ''' <remarks></remarks>

   Private Sub Page_Load(sender As Object, e As System.EventArgs)

       Try

           If Not IsPostBack Then

               ' Get users from DNN to populate the dropdown.

               Dim allUsers As ArrayList = UserController.GetUsers(Me.PortalId)

               ' Create an empty list item to use as the default in the Dropdown List.

               Dim emptyItem As New ListItem("Select a user...", "")

               ' Populate the dropdown with the data retrieved.

               ddlUsers.DataSource = allUsers

               ddlUsers.DataTextField = "DisplayName"

               ddlUsers.DataValueField = "UserId"

               ddlUsers.DataBind()

               ' Add our emptyItem to dropdown at index 0

               ddlUsers.Items.Insert(0, emptyItem)

           End If

       Catch exc As Exception

           Exceptions.ProcessModuleLoadException(Me, exc)

       End Try

   End Sub

   ''' <summary>

   '''

   ''' </summary>

   ''' <param name="sender"></param>

   ''' <param name="e"></param>

   ''' <remarks></remarks>

   Protected Sub ddlUsers_SelectedIndexChanged(sender As Object, e As EventArgs)

       ' if the

       If ddlUsers.SelectedItem.Value <> "" Then

           ' Create a new instance of ModuleCommunicationEventArgs

           Dim mcArgs As ModuleCommunicationEventArgs = New ModuleCommunicationEventArgs

           ' Set up the ModuleCommunication arguments

           mcArgs.Sender = "SampleCommunicatorModule - VB"

           mcArgs.Target = " Arbitrary Text"

           mcArgs.Text = "Your Payload Text"

           mcArgs.Type = "Your Custom Type"

           mcArgs.Value = ddlUsers.SelectedItem.Text

 

           RaiseEvent ModuleCommunication(Me, mcArgs)

           'Else

           'lblMessage.Text = "Can't communicate with other modules!"

           ' End If

       End If

   End Sub

 

   Public ReadOnly Property ModuleActions() As ModuleActionCollection Implements IActionable.ModuleActions

       Get

           Dim Actions As New ModuleActionCollection

           Actions.Add(GetNextActionID, Localization.GetString("EditModule", LocalResourceFile),

                       Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)

           Return Actions

       End Get

   End Property

End Class

 

  1. You have completed the setup for the communicator

 

Set up the Listener

 

  1. Create Module Y using the Christoc DotNetNuke 7 VB.Net DAL2 Compiled Module template

  2. In View.ascx add a label will be populated with the response from the IMC communicator in Module X

 

View.ascx:

 

<div>

  <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>

</div>

 

  1. Open View.ascx.vb and add the necessary namespaces to the “Imports section” (see code below)

  2. Under the class declaration, Implement the IModuleListener interface

  3. Setup the OnModuleCommunication routine.  This is where we will set the lblMessage label with text passed from module X.

 

View.ascx.vb:

 

Imports DotNetNuke

Imports DotNetNuke.Entities.Modules.Actions

Imports DotNetNuke.Entities.Modules

Imports DotNetNuke.Services.Exceptions

Imports DotNetNuke.Services.Localization

Imports ServiTech.Modules.IMCTest_Y.Components

Imports DotNetNuke.UI.Utilities

Imports System.Web.UI

' Import the appropriate namespaces necessary for IMC

Imports DotNetNuke.Entities.Modules.Communications

Imports Telerik.Web.UI

 

Partial Class View

   Inherits IMCTest_YModuleBase

   Implements IActionable

   ' Implement the IModuleListener interface

   Implements IModuleListener

 

   ''' -----------------------------------------------------------------------------

   ''' <summary>

   ''' Registers the module actions required for interfacing with the portal framework

   ''' </summary>

   ''' <value></value>

   ''' <returns></returns>

   ''' <remarks></remarks>

   ''' <history>

   ''' </history>

   ''' -----------------------------------------------------------------------------

   Public ReadOnly Property ModuleActions() As ModuleActionCollection Implements IActionable.ModuleActions

       Get

           Dim Actions As New ModuleActionCollection

           Actions.Add(GetNextActionID, Localization.GetString("EditModule", LocalResourceFile), Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)

           Return Actions

       End Get

   End Property

 

   Public Sub OnModuleCommunication(s As Object, e As ModuleCommunicationEventArgs) Implements IModuleListener.OnModuleCommunication

       lblMessage.Text = "Received User Name: " + e.Value

   End Sub

 

End Class

 

  1. Your Listener is now setup and the modules will be able to communicate with one another.

  2. Add both modules to the same page, and test.

 

NOTE: Be sure the “Supports Partial Rendering” option is not enabled for either modules

 

To use with Ajax

 

    Since “supports partial rendering” doesn’t play nice with IMC, we need to setup our Ajax functionality using Update Panels

 

    Module X - View.ascx

 

    Wrap the div with an updatePanel and be sure to set the updateMode to “Always” if you want it to update automatically.  Otherwise, you can set it to “Conditional” but then you will need to call the upModuleX.update()  when you want it to update.

 

<asp:UpdatePanel ID="upModuleX" runat="server" UpdateMode="Always">

      <ContentTemplate>

<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>

 

      </ContentTemplate>

  </asp:UpdatePanel>

 

    

Module Y - View.ascx

 

    Wrap the div with an updatePanel and be sure to set the updateMode to “Always” if you want it to update automatically.  Otherwise, you can set it to “Conditional” but then you will need to call the upModuleY.update()  when you want it to update.

 

<asp:UpdatePanel ID="upModuleY" runat="server" UpdateMode="Always">

      <ContentTemplate>

 

<div>

  <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>

</div>

      </ContentTemplate>

  </asp:UpdatePanel>