Most of the time when you’re rendering web content that content will exist in a container or have a header or a footer, etc. If you’re rendering an HTML publishing field within SharePoint the user could have simply left the field blank (unless you make the field required of course). In this case you probably don’t want to render the container/header/footer if there isn’t going to be any content. So let’s get down to business…
First you have you’re normal code
<h3>Publishing Header</h3> <publishingwebcontrols:richhtmlfield id="PublishingContent1" fieldname="PublishingContent" runat="server"></publishingwebcontrols:richhtmlfield>
This is fine but we don’t want “Publishing Header” showing if there is no content in the field. So what do we do? First drop the following code at the beginning of the “PlaceHolderMain” in your custom page layout.
<script runat="server"> public static bool FieldHasValue(SPListItem item, string fieldName) { if (item.Fields.ContainsField(fieldName) && item[fieldName] != null) { string html = item[fieldName].ToString(); Regex regex = new Regex(@"\s]+))?)+\s*|\s*)/?>", RegexOptions.Singleline); MatchCollection matches = regex.Matches(html); foreach (Match match in matches) { html = html.Replace(match.Value, ""); } html = html.Replace("\n", "").Trim(); return (html.Length > 0); } return false; } </script>
Now we just have to use some inline ASP to check the field before we do our rendering.
<% if (FieldHasValue(item, "PublishingContent")) Response.Write("<h3>Publishing Header</h3>"); %> <publishingwebcontrols:richhtmlfield id="PublishingContent1" fieldname="PublishingContent" runat="server"></publishingwebcontrols:richhtmlfield>
Cory Peters is the Chief SharePoint Architect at Eastridge Technology, a Microsoft Gold Partner in Winston-Salem, NC.




Hi Cory,
What is the regular expression supposed to do? I get the following error.
Server Error in ‘/’ Application.
parsing “\s]+))?)+\s*|\s*)/?>” – Too many )’s.
Andy
Hey Andy,
Sorry about that, looks like some additional formatting was adding when I did my migration to my new blog, very strange. I’ll get this updated asap.
This Regex is designed to remove empty HTML tags so that a control that only holds
or
will be validated as empty… but as you pointed out it’s broken, I’m not sure what happened with the migration as the regex above doesn’t even look remotely like what was originally there. Hope to have your fix shortly.
Hi Cory,
I tried to follow what you have presented here and I am a little confused…I’m new to sharepoint but I’m an experienced web developer (well on the front end of things) I know CSS and XHTML and javascript.
How is the H3 tag getting populated if the publish control is not in it?
Well anyway I have the following code in my page layout:
I do not want the H2 tag to be present if it is empty…As you know it will cause spacing issues. How do I do that using your code?
I also have the following code on my page and I do not want the whole DL tag outputting if there is no content in both the DT and DD tags. How do I use your code for that?
Thank you ahead of time
Hey George,
Here is one option…
< %
if (FieldHasValue(item, "PublishingContent"))
Response.Write("<h3>“);
%>
<publishingwebcontrols:richhtmlfield id=”PublishingContent1″ fieldname=”PublishingContent” runat=”server”></publishingwebcontrols:richhtmlfield>
< %
if (FieldHasValue(item, "PublishingContent"))
Response.Write("</h3>“);
%>
This way the <h3> tag is only rendered around the content if the field does have a value.
Hi Cory,
Using the code you have provided I get this error:CS0246: The type or namespace name ‘SPListItem’ could not be found (are you missing a using directive or an assembly reference?)
What do I do about this?
where is the variable “item” initialized becuase I get error CS0103
I put this directive on the page:
I also have the same question about the item variable.
This line was omitted:
Import Namespace=”Microsoft.SharePoint”