Search code examples
dotnetnuke

Give view access permission to DNN module when copying the module from another page


I'm copying the list of modules from another page in DNN. While doing this I have to set the view permission for all users to view the modules. Even the other page(copying from) module setting is different. Below is the example where I set other properties.

ModuleInfo newModule = new ModuleInfo();
0newModule.ModuleDefID = module.ModuleDefID;
newModule.AllTabs = false;
newModule.PortalID = PortalId;
newModule.ContentItemId = module.ContentItemId;
newModule.ModuleTitle = module.ModuleTitle;
newModule.PaneName = module.PaneName;
newModule.TabID = NewTabInfo.TabID;
newModule.ContainerSrc = module.ContainerSrc;
newModule.ModuleOrder = module.ModuleOrder;
newModule.DisplayPrint = module.DisplayPrint;
newModule.DisplayTitle = module.DisplayTitle;
newModule.IsShareable = module.IsShareable;
newModule.IsShareableViewOnly = module.IsShareableViewOnly;
newModule.ModulePermissions=module.ModulePermissions;
newModule.IsWebSlice = module.IsWebSlice;
newModule.WebSliceTitle = module.WebSliceTitle;
newModule.Footer = module.Footer;
newModule.Header = module.Header;

Solution

  • Here a snippet to dynamically add role permissions to a dotnetnuke module in code behind.

    //get the current ModuleInfo 
    ModuleInfo newModule = ModuleController.Instance.GetModule(ModuleId, TabId, false);
    //or create a new module
    ModuleInfo newModule = new ModuleInfo();
    
    //clear the old permissions
    newModule.ModulePermissions.Clear();
    
    //add admin view permission
    ModulePermissionInfo modulePermissionInfo1 = new ModulePermissionInfo();
    modulePermissionInfo1.ModuleID = ModuleId;
    modulePermissionInfo1.AllowAccess = true;
    //view permission id, 1 = view, 2 = edit
    modulePermissionInfo1.PermissionID = 1;
    //administrator role id (from Roles table in dnn database)
    modulePermissionInfo1.RoleID = 0;
    
    //add admin edit permission
    ModulePermissionInfo modulePermissionInfo2 = new ModulePermissionInfo();
    modulePermissionInfo2.ModuleID = ModuleId;
    modulePermissionInfo2.AllowAccess = true;
    //view permission id, 1 = view, 2 = edit
    modulePermissionInfo2.PermissionID = 2;
    //administrator role id (from Roles table in dnn database)
    modulePermissionInfo2.RoleID = 0;
    
    //add all users view permission
    ModulePermissionInfo modulePermissionInfo3 = new ModulePermissionInfo();
    modulePermissionInfo3.ModuleID = ModuleId;
    modulePermissionInfo3.AllowAccess = true;
    //edit permission id, 1 = view, 2 = edit
    modulePermissionInfo3.PermissionID = 1;
    //all users role id (from Roles table in dnn database)
    modulePermissionInfo3.RoleID = -1;
    
    //add the ModulePermissionInfo to the module
    newModule.ModulePermissions.Add(modulePermissionInfo1);
    newModule.ModulePermissions.Add(modulePermissionInfo2);
    newModule.ModulePermissions.Add(modulePermissionInfo3);
    
    //save the permissions
    ModulePermissionController.SaveModulePermissions(newModule);
    
    //clear the dnn cache (if it is the current module, not a new one)
    DotNetNuke.Common.Utilities.DataCache.ClearModuleCache(TabId);
    DotNetNuke.Common.Utilities.DataCache.ClearTabsCache(PortalId);
    DotNetNuke.Common.Utilities.DataCache.ClearPortalCache(PortalId, false);