Actually that does not quite get you the desired ModuleID, only the DesktopModuleID which has a whole different purpose. Here are several points to keep in mind when getting information about a module:
1. A module definition's FriendlyName can be changed on the Host-->ModuleDefinitions page. Several DNN versions back, a new module defintion property was added, the ModuleName. This is not supposed to change - and, in fact, recent DNN versions dis-able this the text box for ModuleName on the ModuleDefinitions page. So, it is much safer to search for a module definition based on ModuleName rather than FriendlyName. The DesktopModuleController now provides the following method:
Function GetDesktopModuleByModuleName(ByVal ModuleName As String) As DesktopModuleInfo
The method which you used indirectly calls GetDesktopModuleByFriendlyName which uses the FriendlyName rather than ModuleName for its search.
2. For a given PortalID, there can be one or many module instances all having the same FriendlyName or ModuleName located on one or many pages (tabs). Think how many times the core Html/Text module appears on a page or portal.
3. Multiple occurances of modules having the same FriendlyName or ModuleName may have different ModuleID's (when placed with the Add Module command) or with the SAME ModuleID (but diferent TabModuleID's) when placed with the Add Existing Module command.
If I may assume that your requirement is to obtain the ModuleID for the instance of a module which you know occurs once and only once in the Portal then the following will work provided you can trust the FriendlyName not to have changed from its installation:
Function GetModuleID(ByVal TargetModuleFriendlyName As String) As Integer
Dim mc As New ModuleController
Dim mi As ModuleInfo = mc.GetModuleByDefinition(PortalId, TargetModuleFriendlyName)
If mi Is Nothing Then
Return -1
Else
Return mi.ModuleID
End If
End Function
A better method (and a variation of the one that I actually use which returns multiple matches in a dictionary object with ModuleTitle as Key and ModuleInfo object as Value) allows you to specify the TabID on which the module instance is located and the ModuleName rather than FriendlyName:
Function GetModuleID(ByVal TargetTabID As Integer, ByVal TargetModuleName As String) As Integer
Dim dmc As New DesktopModuleController
Dim dmi As DesktopModuleInfo = dmc.GetDesktopModuleByModuleName(TargetModuleName)
If Not dmi Is Nothing Then
Dim mc As New ModuleController
Dim modList As ArrayList = mc.GetModulesByDefinition(PortalId, dmi.FriendlyName)
For Each mi As ModuleInfo In modList
If mi.TabID = TargetTabID Then Return mi.ModuleID
Next
End If
Return -1
End Function
Note that this last method could still return only the first of potentially multiple matches with the same TabID and ModuleID but different TabModuleID's.