Constructing Dynamic Navigation and Displaying Breadcrumbs

Most CMS sites dynamically generate their navigation based around the channel and posting structure, the current user's rights, the PublishingMode that user is working in, and relevant properties of the Context, Postings, and Channels surrounding the content being viewed. Special properties like DisplayName and IsHiddenModePublished aid in the delivery of this navigation. Usually this code is placed into a user control so that it can be used on templates throughout the Web Property.

NOTE: Although briefly mentioned here, dynamic navigation and breadcrumbs are covered in depth in Chapter 14.


The beauty of this model is that as the site grows and users change the content and structure, your navigation, which is based on the channel hierarchy and postings within it, immediately reflects the changes to the site. Entire sections of the site can be moved or renamed, and the site navigation will adjust. No more broken links.

Navigation can be created so that it is role specific or based upon the presence or absence of a specific condition or value. It can be as static or dynamic as the site requires.

Things can get a little bit more difficult on a mixed (ASP and ASP.NET) site or one that requires some non-template-based pages. However, these obstacles can also be overcome.

Because dynamic site navigation requests so many objects for each posting served, it can potentially degrade performance. To improve the performance of templates that contain dynamic navigation, the developer should enable output caching. Also, a common rule of thumb is when the number of channels and postings in a given channel exceeds a few hundred, performance issues when rendering navigation may arise. It would be better to have your information architects restructure the site to better accommodate the actual use.

One common technique of presenting the location of the current content within the hierarchy is the use of something called breadcrumbs. Basically, the idea is to show the successive parent of any content until a top-level channel is reached. Traversing the ancestry of a posting can be done both sequentially and recursively. These two methods are the final coding example explored in this chapter.

Ensure that the following two functions, which fundamentally perform the same function, are entered above the Button1_Click function of our Scratchpad template file. First enter the function to build the breadcrumb sequentially:

 private string SequentialBreadcrumb(Channel cmsLeafChannel,   string crumbSeperator) {   try   {     //1. String variable to hold the breadcrumb as it is built     string growingBreadcrumb = null;     //2. Declare a Channel variable to hold the current Channel     //   and initially populate it with the leaf channel     Channel cmsChannel = cmsLeafChannel;     //3. Loop until we get to the Top Channel     while(!cmsChannel.IsRoot)     {       //4. Concatenate an anchor tag including the name of this       //   Channel to the beginning of the growing string       growingBreadcrumb = "<a href='" + cmsChannel.Url + "'>" +         cmsChannel.DisplayName.ToString() + "</a>" +         growingBreadcrumb;       //5. Check if the next channel is the Top Channel       if(!cmsChannel.Parent.IsRoot)       {         //6. If not, include the separator         growingBreadcrumb = crumbSeperator + growingBreadcrumb;       }       //7. Set the current Channel to its own parent       cmsChannel = cmsChannel.Parent;     }     //8. Return the completed breadcrumb string     return growingBreadcrumb;   }   catch(Exception eError)   {     //9. Provide error feedback to the developer     Label1.Text = "<b>Error: </b>" + eError.Message.ToString();     //10. Return the empty breadcrumb string     return string.Empty;   } } 

Then enter the function to build the breadcrumb recursively:

 private string RecursiveBreadcrumb(Channel cmsChannel,   string crumbSeperator) {   try   {     //1. We have reached the top ancestor     if(cmsChannel.Parent.IsRoot)     {       //2. Pop an anchor tag including the name of the Top Channel       return "<a href='" + cmsChannel.Url + "'>" +         cmsChannel.Name.ToString() + "</a>";     }     else     {       //3. Call this function recursively, on the way back       //   pop an anchor tag including the name of this Channel       //   concatenated to the end of the growing string       return RecursiveBreadcrumb(cmsChannel.Parent,         crumbSeperator) +         crumbSeperator + "<a href='" + cmsChannel.Url + "'>" +         cmsChannel.Name.ToString() + "</a>";     }   }   catch(Exception eError)   {     //4. Provide error feedback to the developer     Label1.Text = "<b>Error: </b>" + eError.Message.ToString();     //5. Return the empty breadcrumb string     return string.Empty;   } } 

Replace the Button1_Click function of our Scratchpad template file with the following code:

 private void Button1_Click(object sender, System.EventArgs e) {   try   {     //1. Grab the current CMS Context     CmsHttpContext cmsContext = CmsHttpContext.Current;     //2. Grab the Channel for which to display a breadcrumb     //   from the fully qualified path in the TextBox     //   Cast the result of the Searches object as a Channel     Channel cmsChannel =       cmsContext.Searches.GetByPath(TextBox1.Text)       as Channel;     //3. If the Channel was successfully found it will not be null     if(cmsChannel != null)     {       //4. Populate the Label with the results of both the       //   recursive function and the sequential function       Label1.Text = "<b>Sequential: </b>" +         SequentialBreadcrumb(cmsChannel, " > ") +         "<br><b>Recursive: </b>" +         RecursiveBreadcrumb(cmsChannel, " > ");     }     else     {       //5. Provide nonerror feedback to the developer       Label1.Text = "<b>Channel not found </b>";     }   }   catch(Exception eError)   {     //6. Provide error feedback to the developer     Label1.Text = "<b>Error: </b>" + eError.Message.ToString();   } } 

Build the solution and then refresh the Scratchpad posting in Internet Explorer, or browse to it, type the full path to any valid channel in your CMS site into the text box (we use /Channels/Scratch/Brady/Alice/ based upon the channel hierarchy created earlier in this chapter) and click the Button. The page should reload and look similar to Figure 25-19.

Figure 25-19. Breadcrumb display

graphics/25fig19.gif

Each of the nodes in the breadcrumb represents a clickable URL.



Microsoft Content Management Server 2002. A Complete Guide
Microsoft Content Management Server 2002: A Complete Guide
ISBN: 0321194446
EAN: 2147483647
Year: 2003
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net