Hiding elements on a publishing page when fields are blank

This has been something that bugs me about most page layouts I have done before – you have a field like the description or something like that and when you put it in you have to put it in a <p> tag to get it to render properly. The problem then is that if you don’t populate the field you then don’t want the <p> tag outside the element to render otherwise you will have some unwanted white space. I had a quick Google around and found a pretty good solution for this here.

Essentially the solution involves creating a custom control based on the security trimmer control, and telling it to trim the components inside it when the specific field is blank. I found the code example at that URL didn’t quite work, so here is my code with some minor adjustments made:

public class HideWhenBlank : SPSecurityTrimmedControl
{
    public String FieldNameToCheck { get; set; }   

    protected override void Render(System.Web.UI.HtmlTextWriter output)
    {
        SPListItem item = null;
        WebPartDisplayMode mode = ((SPWebPartManager)this.Page.Master.FindControl("wpmMain")).DisplayMode;   if (mode.Name == "Design")
        {
            base.Render(output);
        }
        else if (SPContext.Current != null)
        {
            item = SPContext.Current.ListItem;
            if (!String.IsNullOrEmpty(FieldNameToCheck) && item != null)
            {
                if (!ValueIsNullOrBlank(item[FieldNameToCheck]))
                {
                    base.Render(output);
                }
            }
        }
    }

    private Boolean ValueIsNullOrBlank(Object data)
    {
        if (data == null || String.IsNullOrEmpty(Convert.ToString(data).Trim()))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

Compile this class and you can go and use this one in your page layouts like this:

<MyCustomTagPrefix:HideWhenBlank runat="server" FieldNameToCheck="Comments">
<p>
<!-- Some sort of custom publishing element in here -->
</p>
</MyCustomTagPrefix:HideWhenBlank>

So basically in this example when the comments field is empty, then the <p> tag as well as anything else inside the custom tag won’t be rendered. The tag will always allow the content to be rendered in edit mode though which means you can always add a value later. Thsi is nice and easy to implement and can help tidy up your page layouts a bit.

    • Gmyster
    • March 9th, 2009

    This is exactly what I need, however, I’m new to sharepoint and I do not know how to implement this. It may seem to you a stupid question but how do I compile the above code and to where exactly do I put the compiled code?

    Thank you for any assistance you can give me. I have Visual Studio 2008 and Sharepoint Designer.

    • Just put the C# code into a class in a DLL that you can deploy to the server (use a WSP file to deploy it, I don’t think I have covered that in an article on my blog but Google around and you will find plenty of stuff about it). Then you can use the second chunk of code in your page layouts.

    • Gmyster
    • March 11th, 2009

    Thank you Brian for your suggestion… I am currently trying to do this. I will let you know if this works.

  1. December 21st, 2009