Properties your page document type must have

Always useful functionality for an Umbraco page document

  • Share

There are two properties which are not related directly to page content which are always useful to have on your document type which represent page on your website.

1. Hide in navigation

Very often you need to have some page which is there but not accessible directly from your navigation links. One of the examples for this is 404 Umbraco page.

This page can be managed from Umbraco backend, but for sure you do not want to list it in navigation. It is not the same as 500 error page (which should be static page) because it is not an application exception. You can easily achieve this with a simple property of True/False type. To be more easy to do this you can event have a small extension for IPublishedContent.

        public static bool GetPropertyValueAsBool(this IPublishedContent page, string propertyName)
        {
            bool result = false;
            if (page.HasProperty(propertyName) && 
				page.GetPropertyValue(propertyName) != null && 
				!bool.TryParse(page.GetPropertyValue(propertyName).ToString(), out result))
            {
                if (page.GetPropertyValue(propertyName).ToString() == 1.ToString())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return result;
        }
    
Note

Whole class with IPublishedContent extensions you can find for download in posts for March 2013

2. Page redirect

Sometime you need to include external link in your navigation, or just include some page which is not exactly where you want it to be to look nice in navigation or in your sitemap (for example I use this to redirect to my linkedin profile which url is not so nice and short :)). The easiest way is to use Response.Redirect method. Of course this needs to be manageable from backend.

For this purpose you can add page redirect property. To be more user friendly I use uComponent UrlPicker do define property type.

Pageredirect

This will provide you options to redirect to content page or external URL. Because of these two options you have provided you need to handle property according to link type you have defined for content page.

    if (Model.Content.GetPropertyValue("umbRedirect") != null && 
        !string.IsNullOrEmpty(Model.Content.GetPropertyValue("umbRedirect").ToString()))
    {
        UrlPickerState urlPicker = Model.Content.GetPropertyValue("umbRedirect") as UrlPickerState;
        if (urlPicker != null)
        {
            if (urlPicker.Mode == uComponents.DataTypes.UrlPicker.UrlPickerMode.URL && !string.IsNullOrEmpty(urlPicker.Url))
            {
                Response.Redirect(urlPicker.Url, true);
            }
            else if (urlPicker.Mode == uComponents.DataTypes.UrlPicker.UrlPickerMode.Content && urlPicker.NodeId != null)
            {
                IPublishedContent contentPage = Umbraco.TypedContent(urlPicker.NodeId.Value);
                if (contentPage != null)
                {
                    Response.Redirect(contentPage.Url, true); 
                }
            }
        }
        
    }
    

Include this code snipped in your main razor template template and you will be able to redirect your content page very easy by setting this property.

References

  • Share

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Comments for this article

comments powered by Disqus