Structuring Navigation and Other Page Properties In Kentico 12 MVC
--
As great as it is building sites using the MVC framework in Kentico, I do miss some of the page features we’re spoilt with that previous editions of Kentico has to offer.
Like anything, there are always workarounds to implement the features we have become accustomed to but ensuring the correct approach is key, especially when moving existing Kentico clients onto Kentico 12 MVC edition. Familiarity is ever so important for the longer tenure clients who already have experience using previous versions of Kentico.
One of the features I will be discussing is an approach to implementing basic page properties, such as Navigation:
- Menu caption
- Hide from navigation
- Show in sitemap
There are many other options the traditional version of Kentico offers that is not present in the MVC version, but this can be expanded on further at a later date.
Step 1: Create Base Page Type
Create a new page type called “Page Base”.
The base page type will be the place where we’ll be adding our navigation properties and any other properties we wish to apply across all our site pages. Any new page types you create in the future will inherit the fields from “Page Base”.
The benefit of inheriting fields from “Page Base” is the ability to easily make updates and add new fields that will apply across all your page types. For the moment, we will just be creating three fields:
- PageMenuCaption
- PageShowInSiteMap
- PageMenuItemHideInNavigation
Step 2: Global Event Hooks for Document Insert/Update
This is the point where you will see some synergy between new and old, where I will be utilising Kentico’s own fields from the CMS_Document table to store the values set in our “Page Base” page type.
We will be targeting the following fields from the CMS_Document table:
- DocumentMenuItemHideInNavigation
- DocumentShowInSiteMap
- DocumentMenuCaption
Add the following method to your project:
/// <summary>
/// Sets the "DocumentMenuItemHideInNavigation", "DocumentShowInSiteMap" and "DocumentMenuCaption" fields.
/// </summary>
/// <param name="tnDoc"></param>
public static void SetSitemapAndMenuDisplay(TreeNode tnDoc)
{
tnDoc.SetValue("DocumentMenuItemHideInNavigation", tnDoc.GetBooleanValue("PageMenuItemHideInNavigation", false));
tnDoc.SetValue("DocumentShowInSiteMap", tnDoc.GetBooleanValue("PageShowInSiteMap", false));
tnDoc.SetValue("DocumentMenuCaption", tnDoc.GetStringValue("PageMenuCaption", string.Empty));
tnDoc.Update();
}
This method is to be called by the Global Event handler’s DocumentEvents.Insert.After and DocumentEvents.Update.After events in the following manner:
private void Document_Update_After(object sender, DocumentEventArgs e)
{
TreeNode doc = e.Node;
#region Set Menu and Sitemap
GlobalEventFunctions.SetSitemapAndMenuDisplay(doc);
#endregion
}
Going down this route is the most efficient approach from a development perspective, as we would be able to query these fields directly via Kentico’s existing API when it comes to outputting our pages within our website navigation build.
NOTE: You could use Kentico’s own Page Fields in a page type instead of creating a Global Event handler to map the custom created fields as I have done above. But as I have been working on integrating more of Kentico’s page related features, I have come across complexities that required data to be manipulated that could only be done at Global Event level. This will make more sense when I detail this further in a future post.
End Result
When editing a page, a “Navigation” section will now be shown.
I am interested in seeing your own approach to adding page level properties. Please leave a comment!
What’s Next?
Since we have defined our page structure to easily add additional properties along with a Global Event to help if we need to do any further data manipulation, which will prove useful for when implementing more complex features, such as Menu design and actions.
Syndicut is a creative and digital agency with extensive knowledge and experience in Kentico CMS, EMS and Kentico Cloud. Over the last 4 years, they have won 6 site of the month awards, 5 site of the year finalists and the first Kentico Cloud award for Best Integration.