First time at my blog? Check out the table of contents!
posted on Wednesday, November 07, 2007 9:15 PM | Filed Under [ DotNetNuke ]

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.

image

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:

image

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.

image

2. Create an instance of a TabPermissionGrid on your page:

image

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.

image

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.

Comments Leave Yours...
Vadim
2/11/2008 4:13 PM
# re: Programmatically Creating DotNetNuke Pages

A question (sorry, I am too lazy to dig DNN source code):
when you call
controller.AddTab(newTab);
is the page persists in the database? Or, it exists for the current session only? I guess it is the former, though.

Rafe Kemmis
2/12/2008 5:13 AM
# re: Programmatically Creating DotNetNuke Pages

Yes, the page exists in the database once AddTab is called. However it might not show up in menus until the cache is cleared.

Shane Haylock
7/3/2008 9:48 AM
# re: Programmatically Creating DotNetNuke Pages

Very helpful tips which I'll be able to use thanks. Could you tell me if there is any way of making a tab/page that is appearing as a menu invisible from code in a module. For some pages I need to make a menu invisible depending on what is selected in one page and passed to another.

Davide
7/18/2008 8:31 AM
# re: Programmatically Creating DotNetNuke Pages

Very nice and useful article!

Thanks
Davide

Vengaboys
7/31/2008 12:51 PM
# re: Programmatically Creating DotNetNuke Pages

I am following the series of blog posts on this blog: http://dnchannel.blogspot.com/2008/07/programmatically-creating-dotnetnuke.html, that talks about how to create a portal using the DotNetNuke API.

alesian
9/3/2008 11:19 AM
# re: Programmatically Creating DotNetNuke Pages

That was so help full for me.I am a dotnet developer,daily i face lot of problems in my work.This article helped me a lot.Can you tell me how to make invisible the frame when page loads.
============================
alesian

http://www.applydnn.com

Shira
10/2/2008 8:14 AM
# re: Programmatically Creating DotNetNuke Pages

Thanks a lot! This post was very very helpful!

Vin
11/9/2008 3:16 AM
# re: Programmatically Creating DotNetNuke Pages

Nice..
Do you know anything about

controller.UpdateTab(objtabinfo) ?

I use you method to create a tab for updating tab..
but it always throws me an error or Violation of UNIQUE KEY in dbo.tabpermission

Any idea?

Decorion Jonathan
1/8/2009 1:54 AM
# re: Programmatically Creating DotNetNuke Pages

its very helpful, thnk your very much.

JK
2/11/2009 9:49 PM
# re: Programmatically Creating DotNetNuke Pages

If I create tab pages programatically in my session since they are ppersits in database, will those tab pages be visible to other users logged in during that time.

sumit
3/12/2009 3:10 AM
# re: Programmatically Creating DotNetNuke Pages

Thank man! Thanks alot
i was Missing on DataCache.ClearModuleCache(newTab.TabID);
and that did the trick

Mark
5/28/2009 1:00 PM
# re: Programmatically Creating DotNetNuke Pages

Thanks for the well written tutorial, especially the time saving use of the TabPermissionsGrid control. That is too cool for school.

For the readers, here is a way to return a success message with a link to the new page opening in a new window. (Assuming you add an label with ID of lblResults to your ascx.)

lblResults.Text = Now.ToShortDateString + " " + Now.ToShortTimeString + _
" Copy complete. <a target=""_blank"" href=""" + Globals.NavigateURL(objTab.TabID) + """>View the new page."

Post Your Comment

Title
Required
Name
Required
Email
Optional
Url
Optional
Comment  
Please add 2 and 4 and type the answer here:

Who Is Rafe

rafe

Rafe Kemmis

I am an audacious web developer with a double bachelor of science in Computer Science and Mathematics. I specialize in Microsoft ASP.Net, Silverlight, and Adobe ActionScript.

Questions?

Always a thoughtful response. You may post your question on an article, or contact me directly.

Hire Me.

I provide custom solutions to complex problems. I can help your business no matter how large or small.

Contact me now.

Subscribe


Join me at CodeStock June 26-27, Knoxville, TN