【问题标题】:How to programmatically create DotNetNuke pages?如何以编程方式创建 DotNetNuke 页面?
【发布时间】:2010-11-29 20:56:56
【问题描述】:

我正在开发一个将用作商店的 DotNetNuke 网站。每个产品都有自己的 DotNetNuke 页面,其中包含各种模块。我想创建一个允许管理员创建新产品的模块。

解决此问题的最佳方法是什么?您如何以编程方式创建新的 DotNetNuke 页面、使用模块填充它们并配置模块?

我确信这可以完成,我只是不知道如何开始。

【问题讨论】:

    标签: dotnetnuke


    【解决方案1】:

    几天后,我终于找到了自己问题的答案。如果其他人正在寻找一种以编程方式创建 DotNetNuke 页面、使用模块填充它们并配置模块的方法——这就是您的解决方案!

    Rafe Kemmis 的This blog entry 是我的起点,尽管它基于早期版本的 DNN。他还有其他几篇 DNN 文章也可能有用。

    以下代码的另一部分取自 DNN 源。我做了一些调整,但它仍然是 99% 的源代码。

    以下是我在解决此问题时组装的一个测试 DNN 模块。希望对你有帮助。弄清楚这一点当然具有教育意义。

    Imports System.Web.UI
    Imports System.Collections.Generic
    Imports System.Reflection
    
    Imports DotNetNuke
    Imports DotNetNuke.Security.Permissions
    Imports DotNetNuke.Services.Exceptions
    Imports DotNetNuke.Services.Localization
    Imports DotNetNuke.Entities.Modules
    Imports DotNetNuke.Entities.Modules.Definitions
    Imports DotNetNuke.Entities.Users
    
    Namespace DNNTest.Modules.PageGenerator
    
        Partial Class ViewPageGenerator
            Inherits Entities.Modules.PortalModuleBase
    
    #Region "Enums"
    
            Private Enum ViewPermissionType
                View = 0
                Edit = 1
            End Enum
    
    #End Region
    
    #Region "Events"
    
            Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                Try
                    If (Not IsPostBack) Then
                        PermGrid.TabID = -1
                        ParentsDropDownList.DataSource = TabController.GetPortalTabs(PortalId, TabId, True, False)
                        ParentsDropDownList.DataBind()
                    End If
    
                Catch ex As Exception
                    'failure
                    Exceptions.ProcessModuleLoadException(Me, ex)
                End Try
            End Sub
    
            Protected Sub btnAddPage_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddPage.Click
                CreatePage(Trim(txtTabName.Text), Trim(txtTabTitle.Text), Trim(txtTabDesc.Text), Trim(txtTabKeyWords.Text), PermGrid.Permissions)
            End Sub
    
    #End Region
    
    #Region "DNN Page Creation"
    
            Private Sub CreatePage(ByVal PageName As String, ByVal PageTitle As String, ByVal Description As String, ByVal Keywords As String, ByVal Permissions As TabPermissionCollection, Optional ByVal LoadDefaultModules As Boolean = True)
                Dim controller As New TabController
                Dim newTab As New Tabs.TabInfo
                Dim newPermissions As TabPermissionCollection = newTab.TabPermissions
                Dim permissionProvider As PermissionProvider = permissionProvider.Instance
                Dim infPermission As TabPermissionInfo
    
                ' set new page properties
                newTab.PortalID = PortalId
                newTab.TabName = PageName
                newTab.Title = PageTitle
                newTab.Description = Description
                newTab.KeyWords = Keywords
                newTab.IsDeleted = False
                newTab.IsSuperTab = False
                newTab.IsVisible = True
                newTab.DisableLink = False
                newTab.IconFile = ""
                newTab.Url = ""
                newTab.ParentId = CInt(ParentsDropDownList.SelectedValue)
    
                ' create new page
                controller.AddTab(newTab, LoadDefaultModules)
    
                ' copy permissions selected in Permissions collection
                For index As Integer = 0 To (Permissions.Count - 1)
                    infPermission = New TabPermissionInfo
    
                    infPermission.AllowAccess = Permissions(index).AllowAccess
                    infPermission.RoleID = Permissions(index).RoleID
                    infPermission.RoleName = Permissions(index).RoleName
                    infPermission.TabID = Permissions(index).TabID
                    infPermission.PermissionID = Permissions(index).PermissionID
    
                    'save permission info
                    newPermissions.Add(infPermission, True)
                    permissionProvider.SaveTabPermissions(newTab)
                Next index
    
                'create module on page
                'CreateModule(newTab.TabID, "MyHTMLModule", "ContentPane", "Text/HTML")
                CreateModule(newTab.TabID, "MyModule", "ContentPane", "Custom_DNN_Module")
    
                ' clear the cache
                DataCache.ClearModuleCache(newTab.TabID)
            End Sub
    
            Private Sub CreateModule(ByVal TabID As Integer, ByVal modTitle As String, ByVal paneName As String, ByVal modDefinitionFriendlyName As String)
                'Create a completely new module on the new page 
                Dim modDefInfo As ModuleDefinitionInfo = ModuleDefinitionController.GetModuleDefinitionByFriendlyName(modDefinitionFriendlyName)
                AddNewModule(TabID, modTitle, modDefInfo.DesktopModuleID, paneName, 0, ViewPermissionType.View, "")
    
                'Configure Module Settings
                ConfigModuleSettings(ModuleID)
            End Sub
    
            Private Sub ConfigModuleSettings(ByVal ModuleID As Integer)
                Dim objModules As New ModuleController
                Dim TabModuleID As Integer = objModules.GetModule(ModuleID).TabModuleID
                objModules.UpdateTabModuleSetting(TabModuleID, "MySetting", "1")
            End Sub
    
    #Region "From DNN Source --mostly"
    
            ''' -----------------------------------------------------------------------------
            ''' <summary>Adds a New Module to a Pane</summary>
            ''' <param name="align">The alignment for the Module</param>
            ''' <param name="desktopModuleId">The Id of the DesktopModule</param>
            ''' <param name="permissionType">The View Permission Type for the Module</param>
            ''' <param name="title">The Title for the resulting module</param>
            ''' <param name="paneName">The pane to add the module to</param>
            ''' <param name="position">The relative position within the pane for the module</param>
            ''' -----------------------------------------------------------------------------
            Private Sub AddNewModule(ByVal TabID As Integer, ByVal title As String, ByVal desktopModuleId As Integer, ByVal paneName As String, ByVal position As Integer, ByVal permissionType As ViewPermissionType, ByVal align As String)
    
                Dim objTabController As New TabController
                Dim objTabPermissions As TabPermissionCollection = objTabController.GetTab(TabID, PortalId, True).TabPermissions
                Dim objPermissionController As New PermissionController
                Dim objModules As New ModuleController
                Dim objModuleDefinition As ModuleDefinitionInfo
                Dim objEventLog As New Services.Log.EventLog.EventLogController
                Dim j As Integer
    
                Try
                    Dim desktopModule As DesktopModuleInfo = Nothing
                    If Not DesktopModuleController.GetDesktopModules(PortalSettings.PortalId).TryGetValue(desktopModuleId, desktopModule) Then
                        Throw New ArgumentException("desktopModuleId")
                    End If
                Catch ex As Exception
                    LogException(ex)
                End Try
    
                Dim UserId As Integer = -1
                If Request.IsAuthenticated Then
                    Dim objUserInfo As Users.UserInfo = UserController.GetCurrentUserInfo
                    UserId = objUserInfo.UserID
                End If
    
                For Each objModuleDefinition In ModuleDefinitionController.GetModuleDefinitionsByDesktopModuleID(desktopModuleId).Values
                    Dim objModule As New ModuleInfo
                    objModule.Initialize(PortalSettings.PortalId)
    
                    objModule.PortalID = PortalSettings.PortalId
                    objModule.TabID = TabId
                    objModule.ModuleOrder = position
                    If title = "" Then
                        objModule.ModuleTitle = objModuleDefinition.FriendlyName
                    Else
                        objModule.ModuleTitle = title
                    End If
                    objModule.PaneName = paneName
                    objModule.ModuleDefID = objModuleDefinition.ModuleDefID
                    If objModuleDefinition.DefaultCacheTime > 0 Then
                        objModule.CacheTime = objModuleDefinition.DefaultCacheTime
                        If Portals.PortalSettings.Current.DefaultModuleId > Null.NullInteger AndAlso Portals.PortalSettings.Current.DefaultTabId > Null.NullInteger Then
                            Dim defaultModule As ModuleInfo = objModules.GetModule(Portals.PortalSettings.Current.DefaultModuleId, Portals.PortalSettings.Current.DefaultTabId, True)
                            If Not defaultModule Is Nothing Then
                                objModule.CacheTime = defaultModule.CacheTime
                            End If
                        End If
                    End If
    
                    Select Case permissionType
                        Case ViewPermissionType.View
                            objModule.InheritViewPermissions = True
                        Case ViewPermissionType.Edit
                            objModule.InheritViewPermissions = False
                    End Select
    
                    ' get the default module view permissions
                    Dim arrSystemModuleViewPermissions As ArrayList = objPermissionController.GetPermissionByCodeAndKey("SYSTEM_MODULE_DEFINITION", "VIEW")
    
                    ' get the permissions from the page
                    For Each objTabPermission As TabPermissionInfo In objTabPermissions
                        If objTabPermission.PermissionKey = "VIEW" AndAlso permissionType = ViewPermissionType.View Then
                            'Don't need to explicitly add View permisisons if "Same As Page"
                            Continue For
                        End If
    
                        ' get the system module permissions for the permissionkey
                        Dim arrSystemModulePermissions As ArrayList = objPermissionController.GetPermissionByCodeAndKey("SYSTEM_MODULE_DEFINITION", objTabPermission.PermissionKey)
                        ' loop through the system module permissions
                        For j = 0 To arrSystemModulePermissions.Count - 1
                            ' create the module permission
                            Dim objSystemModulePermission As PermissionInfo
                            objSystemModulePermission = CType(arrSystemModulePermissions(j), PermissionInfo)
                            If objSystemModulePermission.PermissionKey = "VIEW" AndAlso permissionType = ViewPermissionType.Edit AndAlso _
                                 objTabPermission.PermissionKey <> "EDIT" Then
                                'Only Page Editors get View permissions if "Page Editors Only"
                                Continue For
                            End If
    
                            Dim objModulePermission As ModulePermissionInfo = AddModulePermission(objModule, _
                                                                                    objSystemModulePermission, _
                                                                                    objTabPermission.RoleID, objTabPermission.UserID, _
                                                                                    objTabPermission.AllowAccess)
    
                            ' ensure that every EDIT permission which allows access also provides VIEW permission
                            If objModulePermission.PermissionKey = "EDIT" And objModulePermission.AllowAccess Then
                                Dim objModuleViewperm As ModulePermissionInfo = AddModulePermission(objModule, _
                                                                                    CType(arrSystemModuleViewPermissions(0), PermissionInfo), _
                                                                                    objModulePermission.RoleID, objModulePermission.UserID, _
                                                                                    True)
                            End If
                        Next
    
                        'Get the custom Module Permissions,  Assume that roles with Edit Tab Permissions
                        'are automatically assigned to the Custom Module Permissions
                        If objTabPermission.PermissionKey = "EDIT" Then
                            Dim arrCustomModulePermissions As ArrayList = objPermissionController.GetPermissionsByModuleDefID(objModule.ModuleDefID)
    
                            ' loop through the custom module permissions
                            For j = 0 To arrCustomModulePermissions.Count - 1
                                ' create the module permission
                                Dim objCustomModulePermission As PermissionInfo
                                objCustomModulePermission = CType(arrCustomModulePermissions(j), PermissionInfo)
    
                                AddModulePermission(objModule, objCustomModulePermission, _
                                                                        objTabPermission.RoleID, objTabPermission.UserID, _
                                                                        objTabPermission.AllowAccess)
                            Next
                        End If
                    Next
    
                    objModule.AllTabs = False
                    objModule.Alignment = align
    
                    objModules.AddModule(objModule)
                Next
    
            End Sub
    
            ''' -----------------------------------------------------------------------------
            ''' <summary>Adds a Module Permission</summary>
            ''' <param name="permission">The permission to add</param>
            ''' <param name="roleId">The Id of the role to add the permission for.</param>
            ''' -----------------------------------------------------------------------------
            Private Function AddModulePermission(ByVal objModule As ModuleInfo, ByVal permission As PermissionInfo, ByVal roleId As Integer, ByVal userId As Integer, ByVal allowAccess As Boolean) As ModulePermissionInfo
                Dim objModulePermission As New ModulePermissionInfo
                objModulePermission.ModuleID = objModule.ModuleID
                objModulePermission.PermissionID = permission.PermissionID
                objModulePermission.RoleID = roleId
                objModulePermission.UserID = userId
                objModulePermission.PermissionKey = permission.PermissionKey
                objModulePermission.AllowAccess = allowAccess
    
                ' add the permission to the collection
                If Not objModule.ModulePermissions.Contains(objModulePermission) Then
                    objModule.ModulePermissions.Add(objModulePermission)
                End If
    
                Return objModulePermission
            End Function
    
    #End Region
    
    #End Region
    
        End Class
    
    End Namespace
    

    这是标记...

    <%@ Control Language="vb" Inherits="DNNTest.Modules.PageGenerator.ViewPageGenerator" AutoEventWireup="false" Explicit="True" CodeBehind="ViewPageGenerator.ascx.vb" %>
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    <%@ Register TagPrefix="Portal" Namespace="DotNetNuke.Security.Permissions.Controls" Assembly="DotNetNuke" %>
    <div>
        <table>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Parent Tab:" />
                </td>
                <td>
                    <asp:DropDownList ID="ParentsDropDownList" runat="server" DataTextField="TabName" DataValueField="TabID" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Tab Name:"/>
                </td>
                <td>
                    <asp:TextBox ID="txtTabName" runat="server"/>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="Tab Title:"/>
                </td>
                <td>
                    <asp:TextBox ID="txtTabTitle" runat="server"/>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label4" runat="server" Text="Description:"/>
                </td>
                <td>
                    <asp:TextBox ID="txtTabDesc" runat="server"/>
                </td>
            </tr>
            <tr>
                <td>
                <asp:Label ID="Label5" runat="server" Text="Key Words:"/>
                </td>
                <td>
                    <asp:TextBox ID="txtTabKeyWords" runat="server"/>
                </td>
            </tr>
        </table>
        <br />
        <Portal:TabPermissionsGrid ID="PermGrid" runat="server" />
        <br />
        <asp:Button ID="btnAddPage" runat="server" OnClick="btnAddPage_Click" Text="Add New Page Here" Width="161px" />
    </div>
    

    【讨论】:

    • 旧帖,我知道。这是完整的解决方案吗。不幸的是,该博客已经关闭了一段时间——我一直在寻找这种类型的解决方案。只是想知道这是否是我所需要的(显然已根据我自己的情况进行了调整)。谢谢。 +1
    • 回复Moriarty:我已经有几年没有和DNN合作了,但我记得我发布了需要的关键部分。虽然,我不知道 DNN 在此期间发生了怎样的变化。我希望我的旧解决方案对您有所帮助。
    【解决方案2】:

    我认为最简单的做法是创建一个页面级模板。设置一个页面,其中包含您想要的所有模块。将其保存/导出为模板。然后您需要创建新产品的用户单击添加页面。制作页面。然后只需配置其中包含您的产品/商店逻辑的模块。应该做的伎俩。

    【讨论】:

    • 我考虑过使用模板,但我认为这仍然给用户留下了太多的配置。不过,感谢您的建议。
    • 我不经常使用模板,但我认为如果您的模块支持导出功能,那么您也可以预先配置模块。所以,不确定您为实际产品使用的模块。一般来说,为每个产品创建一个页面似乎是一项疯狂的工作,但我必须假设你有你的理由:]
    • 我不得不说我同意@Ryan Doom - 这似乎是一项疯狂的工作 - 那里的许多 ecomm 软件包会更简单地做到这一点,并且管理会更容易
    猜你喜欢
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多