This is part IV of the Making Your DotNetNuke Module Do More For You series where I attempt to bridge the gap between knowing DotNetNuke module basics and knowing how to add the precise functionality that you ideally would like your modules to have. In this series we are primarily focusing on using the built-in functionality of the DotNetNuke Framework core. In part III I showed you how to use personalization to store unique information for each user: Let’s Talk DotNetNuke User Personalization.
Today we will continue building your page modification skills. We will be exploring the DotNetNuke.Entities.Tabs namespace and using classes from that namespace to programmatically add pages to your site from within a module.
Let’s get to work!
The DotNetNuke.Entities.Tabs Namespace
In the early days of DotNetNuke, the Nuke vernacular used the term “Tab” to refer to a DotNetNuke page. This is why in many places in the framework you will see the term Tab instead of Page. Such is the case with the classes we will work with today.
The DotNetNuke.Entities.Tabs namespace contains a class that represents a page (the TabInfo class), and a class that does a lot of business logic for a page (the TabController class).
DotNetNuke.Entities.Tabs.TabInfo
Take a minute and navigate to the Page Management area of a page in your DotNetNuke site. You can reach this area by clicking on the “Settings” icon in the control panel.
The settings on the Page Management page have an almost one-to-one relationship with the TabInfo class. In fact, when you click “Update” on the bottom of the page, the code inside DotNetNuke’s ManageTabs control parses out the information from the Page Management form and uses the data to set all the properties of a new or existing instance of the TabInfo class. Here is a code snippet from DotNetNuke's ManageTabs control. As you can see, the page properties are truly being shoved right into a TabInfo object:
215 Dim objTab As New TabInfo
216
217 objTab.TabID = TabId
218 objTab.PortalID = PortalId
219 objTab.TabName = txtTabName.Text
220 objTab.Title = txtTitle.Text
221 objTab.Description = txtDescription.Text
222 objTab.KeyWords = txtKeyWords.Text
223 objTab.IsVisible = chkMenu.Checked
224 objTab.DisableLink = chkDisableLink.Checked
225 objTab.ParentId = Int32.Parse(cboTab.SelectedItem.Value)
226 objTab.IconFile = strIcon
227 objTab.IsDeleted = False
228 objTab.Url = ctlURL.Url
229 objTab.TabPermissions = dgPermissions.Permissions
230 objTab.SkinSrc = ctlSkin.SkinSrc
231 objTab.ContainerSrc = ctlContainer.SkinSrc
The same technique can be used by your module. Once you get an instance of the TabInfo class, you can programmatically set all the same page properties that the Page Management page sets.
Here’s a look at all the public properties that the TabInfo class makes available to you:
Most of the properties of TabInfo are self explanatory, but there are a few settings that can leave you scratching your head.
The biggest difficulty I experienced with the TabInfo class was setting the TabPermissions property. This property is of type DotNetNuke.Security.Permissions.TabPermissionCollection. As you probably already guessed, this property exposes the security permissions for the page you are creating or modifying. You use this property to define what users and roles can view and edit the page. How I learned the best way to configure these permissions was to sit through a 2 hour lecture on DotNetNuke page permissions. Luckily you won’t be doing that today, because I found a fast and mindless way to get you going quickly.
You will be using the TabPermissionGrid control that ships with DotNetNuke. Just follow these steps:
1. Use a page directive to register the control so that your page knows what a TabPermissionGrid is.
2. Create an instance of a TabPermissionGrid on your page:
3. From your code-behind, set the TabID property of the TabPermissionGrid to -1.
48 if (!Page.IsPostBack)
49 {
50 PermGrid.TabID = -1;
4. On post-back, grab the Permissions property of the TabPermissionGrid instance. This property happens to be of type TabPermissionCollection, the same type as your TabInfo class’s TabPermissions property).
82 newTab.ParentId = int.Parse(ParentsDropDownList.SelectedValue);
The TabPermissionGrid renders a control that allows you to configure permissions visually in a roles/users/permissions matrix.
Now tell me that is not quick and mindless. I love it.
Learning the nuts and bolts of the DotNetNuke.Security.Permissions namespace isn’t on the menu for today’s post. But subscribe to my feed; I will be covering this topic in an upcoming article.
More TabInfo properties – uncovered!
There are a few more properties that might give you headaches. Let’s review them:
1. TabInfo.IsDeleted – Unless your new page has a deathwish, you should probably set this property to false.
2. TabInfo.IsSuperTab – As great as you might believe your page to be, it probably is not super. Super tabs are pages under the Host Menu Item, like Host Settings, Modules Definitions, and stuff like that. Set this property to false.
3. TabInfo.IsVisible – This is a flag that gets wired up to the “include in menu” checkbox that is on the Page Settings page. Set this to true if you want your page to show up in the menu. Set it to false if you wish to keep your page from being listed in the menu.
4. TabInfo.DisableLink – This option lets you disable the page, which essentially makes your page not available to users of the site. This is the property that gets wired up to the “Disabled” check box in the Page Settings area.
Adding Pages Using the TabController Class
Now that you have populated the TabInfo object that represents your new page, how do you add the page to your site? This is the job of the TabController class.
This class has methods to add, copy, delete, retrieve, and update pages of the site. Today we will only focus on the AddTab method.
323 Public Function AddTab(ByVal objTab As TabInfo) As Integer
Tab.Controller.AddTab() simply takes an instance of a TabInfo class and does all the page magic for you. After AddTab() creates the page, it returns the tab id of the newly created page.
By default, all the pages created using this method receive instances of the modules configured to appear on all pages. If you wish to not have the “all pages” modules appear on the page you are creating you can use an overloaded version of the AddTab() method:
327 Public Function AddTab(ByVal objTab As TabInfo, ByVal AddAllTabsModules As Boolean) As Integer
This version takes the TabInfo object just as the regular version does. The second parameter is a Boolean value used to specify if the “all pages” modules should be shown or not. Pass true to show those modules, or false to not show them on the new page.
Don't Forget to Clear the Module Cache!
One final thing you must do in order for you new page to show up right away is to clear the Module Cache. So don't forget to do it!
90 // clear cache
91 DataCache.ClearModuleCache(newTab.TabID);
All Together Now!
And that’s all there is to programmatically creating DotNetNuke modules! Let's put it all together now:
1 TabController controller = new TabController();
2 TabInfo parentTab = controller.GetTab(
3 int.Parse(ParentsDropDownList.SelectedValue),
4 PortalId,true);
5 TabInfo newTab = new TabInfo();
6
7 newTab.PortalID = PortalId;
8 newTab.TabName = TextBox1.Text.Trim();
9 newTab.Title = TextBox2.Text.Trim();
10 newTab.Description = TextBox3.Text.Trim();
11 newTab.KeyWords = TextBox4.Text.Trim();
12 newTab.IsDeleted = false;
13 newTab.IsSuperTab = false;
14 newTab.IsVisible = true;
15 newTab.DisableLink = false;
16 newTab.IconFile = "";
17 newTab.Url = "";
18 newTab.ParentId = int.Parse(
19 ParentsDropDownList.SelectedValue);
20 newTab.TabPermissions = PermGrid.Permissions;
21
22 controller.AddTab(newTab);
23
24 // clear cache
25 DataCache.ClearModuleCache(newTab.TabID);
Download the Example Code
WorkingWithTabsCS.zip
To Summarize
1. The DotNetNuke.Entities.Tabs namespace is the key to programmatically creating pages. This namespace is where you can find the TabInfo and the TabController classes.
2. The TabInfo class embodies all the properties that you need to set on your new page. Begin by creating a new instance of TabInfo, and proceed to set all its properties.
3. If appropriate, you can leverage the TabPermissionGrid in your page in order to help you populate the Permissions property of your new TabInfo object.
4. To add a page, pass your new instance of TabInfo to the AddTab() method of an instance of the TabController class.
5. Go feed the dog.
Thanks for reading! I hope some readers find this information useful. If you happen to find some inaccuracies in my tutorial or have something to add, then please leave some feedback.
If you’re beginning to get the hang of programming DotNetNuke modules, then stick around. I have some disturbingly mind-blowing information coming up that beginning modules developers won’t want to miss.