Hide property in Umbraco

How to hide property in Umbraco backend

There is no option for hiding a property in Umbraco. Simply, when you define a property for document type, it is assumed that you are going to use it.

However, sometime you need to hide it for some specific condition, like level of document in a tree, or document type which might inherits some property which you do not want to use in inherited document type. Anyway if defined, property will always be there, but at least you can hide it or disable it.

To do this you need to make some small workaround. First you need to define ApplicationHandler class in which you will hook to Umbraco content control load in back-end. 

public class ApplicationHandler : ApplicationEventHandler
{
public ApplicationHandler()
{
ContentControl.AfterContentControlLoad += new ContentControl.AfterContentControlLoadEventHandler(ContentControl_AfterContentControlLoad);
}
private void ContentControl_AfterContentControlLoad(ContentControl contentControl, ContentControlLoadEventArgs e)
        {
			//Here goes handler method code and logic for finding the property control
		}
	}
    

Now, when you are hooked to Umbraco event, you just need to find the control based on property alias and hide it or disable it, but because FindControl in C# does not drill to all controls in a container control you need to pull it out the different way than just using FIndControl.

For this purpose I used some helper methods from Umbraco API. But before trying to get a control we need to pull out the page as IContent. Every content page in Umbraco content edit panel is loaded in editcontent.aspx page to which page id is passed as a Querystring, so we get the page object as following.

int docId = 0;
            int.TryParse(HttpContext.Current.Request["id"], out docId);
            IContent content = ApplicationContext.Current.Services.ContentService.GetById(docId);
    

Now when we have page as IContent we can pull out the control as following

    public class ApplicationHandler : ApplicationEventHandler
    {
        public ApplicationHandler()
        {
            ContentControl.AfterContentControlLoad += new ContentControl.AfterContentControlLoadEventHandler(ContentControl_AfterContentControlLoad);
        }
		
		private void ContentControl_AfterContentControlLoad(ContentControl contentControl, ContentControlLoadEventArgs e)
        {
			int docId = 0;
            int.TryParse(HttpContext.Current.Request["id"], out docId);
            IContent content = ApplicationContext.Current.Services.ContentService.GetById(docId);
			
			Control ctl = umbraco.presentation.LiveEditing.Utility.FindControl<Control>(delegate(Control c)
               {
                   return c.ClientID.EndsWith("propertyAliasToHide");
               }, contentControl.Page);
		}
	}
    

Now, when you have control located you just need to hide it, but as Umbraco property is represented with label + value control and all the wrapped inside property pane control, you have to take the parent of located control for additional two levels up and then hide or disable that control. To make it easier to use I wrapped this code in a method which checks for nulls and avoid NullReferenceException to occur.

		private void HideProperty(Control control)
        {
            if (control != null)
            {
                Control parent = control.Parent;
                if (parent != null)
                {
                    if (parent.Parent != null)
                    {
                        if (parent.Parent.Parent != null)
                        {
                            parent.Parent.Parent.Visible = false;
                        }
                    }
                }
            }
        }
    

It is only left to invoke this method from previously declared event handler method and woila, will work like a charm.

    public class ApplicationHandler : ApplicationEventHandler
    {
        public ApplicationHandler()
        {
            ContentControl.AfterContentControlLoad  = new ContentControl.AfterContentControlLoadEventHandler(ContentControl_AfterContentControlLoad);
        }
		
		private void ContentControl_AfterContentControlLoad(ContentControl contentControl, ContentControlLoadEventArgs e)
        {
			int docId = 0;
            int.TryParse(HttpContext.Current.Request["id"], out docId);
            IContent content = ApplicationContext.Current.Services.ContentService.GetById(docId);
			
			Control ctl = umbraco.presentation.LiveEditing.Utility.FindControl<Control>(delegate(Control c)
               {
                   return c.ClientID.EndsWith("propertyAliasToHide");
               }, contentControl.Page);
			HideProperty(ctl);
		}
		
		private void HideProperty(Control control)
        {
            if (control != null)
            {
                Control parent = control.Parent;
                if (parent != null)
                {
                    if (parent.Parent != null)
                    {
                        if (parent.Parent.Parent != null)
                        {
                            parent.Parent.Parent.Visible = false;
                        }
                    }
                }
            }
        }
	}
    

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.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

.NET

read more

JavaScript

read more

SQL/T-SQL

read more

PowerShell

read more

Comments for this article