DNN Blog

MetaWeblog URL:

Dynamically Change DotNetNuke Skins by Security Role

Return to Previous Page

  • 10/18/2011
  • 1955 Views

Personalized DotNetNuke Skins

There are all kinds of ways to make a skin (or design) in DotNetNuke react in a dynamic or personalized fashion, and it can be done in any number of ways.  To date, my favorite article on how to do this was written by Vassilis Terzopoulos (@thinkofdesign).  As his Hook Your DotNetNuke Skins blog post illustrates, a "user control" style skin allows you to use and reuse the DotNetNuke API in your skin design.  This has potentially limitless potential in customizing the user experience from a very high level across your entire site.

Personalization

One of the many ways to provide a dynamic experience for your website visitors is to “personalize” their experience.  There are many examples of personalization out there, from the iGoogle approach, to targeting content based off of user activity.  No form of personalization is wrong, as long as it adds positive value to the experience that your website visitors have while visiting your site.

I am going to focus on something very specific in terms of personalization for this blog post.  I am going to talk about serving up a specific design to your visitors, based upon the security role (or group) that your visitor belongs to.

Did you know that DotNetNuke has a personalization engine in the API?  It is pretty impressive.  You should take a look and build something cool with it.  ;)

Background:  Loading Skins

There are a few different ways to dynamically or programmatically change the skin for a specific page load.  DotNetNuke will look first for an override value in the URL.  If  specific value is found, then DNN will load that skin and/or container on that page load.  Second, DNN will look in a local cookie to see if there is a skin being defined.  Finally, if the first two methods did not specify a skin to load, DNN will load the default skins defined by the page or site.  In the event that the skin doesn’t exist, the default skin that ships with DNN will be loaded. 

This is why it’s important to not delete the original skin package after installing.

Probably the best way to approach dynamically loading a skin based on security role would be to create a simple cookie using either a DotNetNuke module, or HttpModule.  Either way, you will be able to retrieve the user information, and based on the IsInSecurityRole() property, generate a cookie that will in effect load the desired skin.

The Code

This solution is dependent upon the skins in question already being installed, and knowing which security roles there are to use for this purpose.  I am simply going to provide you the starting point in this blog post by means of a code snippet.  It will be up to you to find a cool way to use this in your own solutions.

Basically, this solution requires that you have access to the current HttpContext object.  This object must have the context of the current web request, or be spoofing it.

The snippet of code below assumes that you want to assign the built-in DarkKnight homepage skin to an imaginary security role you've created called "My Security Role."  Also note that you can use the UserController.GetCurrentUserInfo() method to get the current user object – provided that your code has the current HttpContext.

VB:

' import DotNetNuke.Entities.Users 
If Not Me.UserInfo Is Nothing AndAlso Me.UserInfo.UserID > Null.NullInteger Then
    If Me.UserInfo.IsInRole("My Security Role") Then
        ' import System.Web.HttpCookie 
        Response.Cookies.Add(New HttpCookie("SkinSrc", "GSkins/DarkKnight/Home-Mega-Menu.ascx"))
    Else
        ' either assign another skin, or do nothing 
    End If
Else
    ' either assign another skin, or do nothing 
End If

C#:

// using DotNetNuke.Entities.Users 
UserInfo oUser = UserController.GetCurrentUserInfo();
if (this.UserInfo != null && this.UserInfo.UserID > Null.NullInteger)
{
    if (this.UserInfo.IsInRole("My Security Role"))
    {
        // import/using System.Web.HttpCookie 
        Response.Cookies.Add(new HttpCookie("SkinSrc", "GSkins/DarkKnight/Home-Mega-Menu.ascx"));
    }
    else
    {
        // either assign another skin, or do nothing 
    }
}
else
{
    // either assign another skin, or do nothing 
}

Keep in mind that the above are just your starting points.  You can set this up and write the production code any way that you’d like.  The string value of security role would be pulled from a setting of some kind in the UI, where you would have queried the security roles that are in production.  You would need defensive code that checks to make sure that the security role still exists (people can and will delete it). 

However, the point should be pretty clear to you.  You can define a user specific cookie that loads a skin that is specific to the user.  If you want, you can base this off of user profile properties and pretty much anything else your code has access to.

The G value references the path to skins that are installed in the Host directory.  Skins that are installed for a specific portal would use L instead.

If you want to do the same thing for a container, use a cookie named ContainerSrc instead of SkinSrc.

Now you have all of the information you need to be able to pull off some magic with the design and personalization on your DotNetNuke website.  The rest is up to you. 

With DNN… You’re only limited by your creativity.

This blog post is cross-posted from my personal blog site.

Author:

Will Strohl

Try DNN 7

Will Strohl is an author, ASP.Net architect, developer, and SQL Server DBA in the San Francisco area, specializing in DotNetNuke. Will has held positions ranging from Help Desk Technician, to being the Director of Technology over his 12 years in IT. Now, he is an Evangelist and Sales Engineer for DotNetNuke® Corporation (www.dotnetnuke.com). Will was also the President of the Orlando DotNetNuke® Users Group or ODUG (orlando.dotnetnukeug.net) for 2 years, and the Vice President of the Orlando .Net User Group or ONETUG (www.onetug.org), but now is an active member of the Bay Area DNN User Group or BayDUG (www.baydug.org).

Will regularly speaks at user groups, webinars, and other events about DotNetNuke® and the various ways it can be used and managed, including founding the Day of DotNetNuke®. He also won the INETA Community Champions award for 2008 Q3 and 2009 Q3.  He was first published in the SDN Magazine, writing about Skin Widgets in DotNetNuke.  Will publishes DotNetNuke videos, and is a freelance Technical Editor for Professional DotNetNuke 5 and DotNetNuke 5 User's Guide by Wrox, as well as Wrox Blox, including DotNetNuke and Web Standards, and Done in 60 Minutes: Building a Custom DotNetNuke Membership Provider. Most recently, Will was selected and spoke at the prestigious DNNConnections portion of the DevConnections in Las Vegas in 2010 and DotNetNuke World in 2011.

Attend A Webinar
Start  Professional Edition Trial
Have Someone Contact Me

Like Us on Facebook Join our Network on LinkedIn Follow DNN Corporate on Twitter Follow DNN on Twitter

Advertisers

Sponsors

DNN Blog Archive

Blog Calendar

DotNetNuke Corporation

DotNetNuke (DNN) provides a suite of solutions that make designing, building and managing feature-rich sites and communities fast, easy and cost-effective. The DotNetNuke Platform CMS is the foundation for more than one million websites worldwide. DNN Social, our newest solution, enables businesses to create immersive, interactive communities. Thousands of organizations like True Value Hardware, Bose, Cornell University, Glacier Water, Dannon, Delphi, USAA, NASCAR, Northern Health and the City of Denver have leveraged DNN to deploy highly engaging business- critical websites. Our rapid growth in product sales and deployments resulted in DotNetNuke Corp. being named one of the fastest growing private companies in America by Inc. Magazine in 2011 and 2012.