CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Mark DiGiovanni

Software Development and Supporting Technologies

March 2004 - Posts

  • CMS 2002 Searching: Custom Channel Property Example

    Searching for a channel is a common task in CMS.  There are a few different ways to search in CMS.

    • CmsHttpContext.Current.Searches.GetByGuid
    • CmsHttpContext.Current.Searches.GetByPath
    • CmsHttpContext.Current.Searches.GetByUrl
    • CmsHttpContext.Current.Searches.GetChannelsByCustomProperty

    One of the ways that you cannot search for a channel is by the channel’s name.  This is because multiple channels can exist with the same name.  The best way to search for a channel is by using CmsHttpContext.Current.Searches.GetByGuid.  However, there are occasions when none of these options will work.  (....Current.Searches can be used to search for more than just channels.  Click here for more info)

    Scenario

    • The existing production environment’s architecture cannot change.  The new CMS application must co-exist peacefully without any impact to the existing web applications.
    • The database that drives the business will be used by both environments.
    • The existing system has a way of linking a site to an ID.  This is how customer accounts are associated with each site in the database.  One database for all sites.
    • There will be several CMS sites.  Each new site will map the channel name to the host header name (the sites “main” channel in CMS).  This will allow each CMS aware site to use the same code base and the same set of templates.
    • The site’s ID must be known upfront in order to perform essential business functions.

    The problem that arises is this; what is the best way to find the site’s ID?  A mapping table in a database that maps the URL to an ID is one possibility.  But using a custom channel property is another. Each site can now have a SiteID and since the method walks up the hierarchy, it will always stop when the root channel is reached and the main channel is found.  This will prevent ID's from being mixed-up.

    Since each site will have an ID, we can create a custom property at the top level channel (just below the root) called “SiteID” as shown below.

    The SiteID is not known when the customer first visits.  The customer could potentially enter the site at any point in the hierarchy (if allowed by bookmarking, linking, etc.).  How can the site’s ID be found?  Since there will be many sites within CMS that have a custom property called “SiteID”, searching by the custom property name is not an option.  This would cause each site’s main channel to be returned with the results.

    Solution:

    Create a class that encapsulates the channel and its properties and look for the site’s main channel by iterating up the hierarchy.  Place the following code in the class in the appropriate places:

    using Microsoft.ContentManagement.Publishing;

    //The context:
    protected CmsHttpContext _cmsContext;

    //The constructor:
    public CustomChannelProperties()
    {
    //Set the current context
    _cmsContext = CmsHttpContext.Current;
    }

    //This method will return the site’s ID. Pass in the property that is being searched for. In this case its "SiteID."
    //A call to GetSiteMainChannelPropertyValue is made to retrieve the channel’s custom property collection.
    public string GetSiteMainChannelPropertyValue(string propertyName)
    {
    return GetSiteMainChannelProperties()[propertyName].Value;
    }

    //GetSiteMainChannelProperties will walk up the hierarchy until the site’s main channel is found.
    //Scope this method as public. You may want to reuse this method later to retrieve the entire custom property collection.
    public CustomPropertyCollection GetSiteMainChannelProperties()
    {
    CustomPropertyCollection siteMainCustomProperties;
    //Make sure the current channel is not the root.
    if(_cmsContext.Channel.IsRoot)
    {
    //If this is the root channel, which it should never be, throw an error.
    throw
    new ApplicationException("Custom properties of the root channel is not permitted.");
    }
    else
    {
    //Begin the search, if successful, return the custom properties, else return null.
    Channel siteMainChannel = MainSiteChannelSearch(_cmsContext.Channel);
    if(siteMainChannel != null)
    siteMainCustomProperties = siteMainChannel.CustomProperties;
    else
    siteMainCustomProperties = null;
    }
    return siteMainCustomProperties;
    }

    //This method will walk the hierarchy until the parent is found.
    private Channel MainSiteChannelSearch(Channel parentChannel)
    {
    if(parentChannel.Parent.IsRoot)
    //site's main channel has been found. Return
    return parentChannel;
    else
    //The parent of this channel is not the root so this channel cannot be the site's main channel.
    //Make a recursive call to continue the search up the hierarchy.
    return MainSiteChannelSearch(parentChannel.Parent);
    }

    The value of “SiteID“ is returned to the calling method once the main channel is found.

    This solution does not require the channel’s name nor does it require anything specific other than the property’s name to retrieve the value.  The current channel is known because it is kept in the CmsHttpContext object.

    For more information: CMS 2002, Channel, Searches, CustomPropertyCollection, Newsgroup, MCMS 2002 FAQ

    --Mark

  • Keeping a Journal / iDailyDiary Review

    I like to keep a journal of daily work experiences, primarily things that are directly related to the project that I am working on.  When I run into something that is significant, I click on the icon in the taskbar, press F5(inserts date), and enter the details.  Journaling does not take very long.  It helps me put together a weekly status report and gives me the ability to account for my daily activities if I ever have to justify them.

    The application that I am using is called iDailyDiary.  This application is very easy to use.  The free version actually has features that makes this program usable.

    Free, Trial/full version download page

    The three four things that I like the most:

    • Stays resident in the taskbar for easy access.
    • Clear workspace... no extra junk to get in the way
    • Sticky tabs that are not related to any specific journal entry
    • I can take a screen shot and include it with the journal entry

    Features listed from the iDailyDiary website:
    Key Features

    Bullet
    Data files encrypted and password protected
    Bullet Richtext Editor
    Bullet Fully Searchable
    Bullet Insert Pictures, URL's and Hypertext
    Bullet Multiple "pages" for each day
    Bullet Export to HTML format to create web pages

    iDailyDiary Professional

    The Professional version contains all the great features of the free version plus more:

    Bullet Spell Checker and Thesaurus
    Bullet Sticky Tabs, non-dated entries
    Bullet Templates
    Bullet Print Multiple Pages or entire Diary
    Bullet Print Headers and Footers (and other Print settings)
    Bullet Support for Tables
    Bullet Insert Smileys
    Bullet Insert/Store Files of any kind (including Audio and Video)
    Bullet Manual Backup and Restore diary data files
    Bullet Tree-View style Browser for diary entries, sticky tabs and templates
    Bullet All for just $30 (US)

    There is also a forum available.

    --Mark

  • Visual Studio Tools for the Microsoft Office System 2003 Training

    Microsoft is offering these labs for VS.NET and Office 2003.  I highly recommended these labs for those who plan on or are in the process of .NET development with the Microsoft Office 2003 System tools.

    Darrell, Grant, and I will be presenting a topic along these lines on May 24th at the Ineta Web Cast.

    My previous posts on the Office XP PIAs:
    Excel automation (interop) with the .NET Framework
    WeProgram.NET Presentation: Office XP PIAs

    --Mark

  • How to Enable Remote Debugging on Windows XP Service Pack 2

     This article details the changes need to Enable Remote Debugging on Windows XP Service Pack 2.

    --Mark

  • Great Free Tree View Control - squishyTREE

    Today I was looking for a tree view control for sub navigation in an image gallery.

    Situation:
    Budget for controls: $0
    Time to develop a custom control: 0
    Time to test expensive third party controls: 0
    Finding the perfect control in under 30 minutes: priceless.

    I believe it is good to give credit when credit is due, especially since most people will only comment when they have something negative to say. 

    I did a Google search for free tree view controls.  I eventually made it here: http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=a096c052-520e-4fdd-9e64-9ff0875e59a6

    This control, squishyTREE, was developed by Adam Sills.  This control meets my needs 100 percent!  Full source code with nine samples are provided.  squishyTREE is very easy to use.  It can be wired up to a DataSet, XML file, etc.  I had no problem hooking up my existing CssClass.  If you need a tree view control, check this one out FIRST.  It is far better than many tree view controls that I would be willing to pay for.

    Great job Adam!

More Posts

Our Sponsors

Free Tech Publications

This Blog

Syndication

News