Extending Sitemaps to provide rich page features and controls

On one web project there was a need to turn on and off the ability to print on individual pages. The print control is on the master page and the administrator wishes to be able to adjust which pages may print easily. The solution is actually simple:

 

  • Add a new attribute to the Sitemap node, for illustration we will use a XmlSiteMap as shown below with @mapPrint:
  • <?xml version="1.0" encoding="utf-8" ?>
    <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
        <siteMapNode url="" title=""  description="">
            <siteMapNode url="default.aspx" title="Home Page"  description="" mayPrint="true" />
            <siteMapNode url="about.aspx" title="About Us"  description=""  mayPrint="false"/>
        </siteMapNode>
    </siteMap>
  • Add a static class and method to the application:
  • public static class Utility
    {
        public static bool MayPrint(this SiteMapNode node)
        {
            return node[ConfigurationManager.AppSettings["mayPrint"] ?? "mayPrint"] != "false";        
        }
    }
  • In the master page, just read the value!
  • public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PrintControl.Visible = System.Web.SiteMap.Provider.CurrentNode.MayPrint();
        }
    }

You will notice that I actually have not hard coded the attribute name. I have provided a default value which I may alter from web.config as needed. Items like this I would usually add to a Web Server Control Library so I can reuse the code everywhere.

 

This same pattern may be used for any other information that you wish to associate with specific pages. The mechanism is simple and can often eliminate a stack of spagetti coding (which may be hard-coded to make it worst).

Comments

  1. It is worth pointing out that the point of the static class is to create an Extension method (http://msdn.microsoft.com/en-us/library/bb383977.aspx) for SiteMapNode.

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple WP7 Mango App for Background Tasks, Toast, and Tiles: Code Explanation

Yet once more into the breech (of altered programming logic)

How to convert SVG data to a Png Image file Using InkScape