Check if pages field type is empty or not

Corey Megown asked on August 21, 2018 23:51

Hello,

I have a custom page type that has a "pages" field to allow authors to select from a folder of widgets. They can select one or more of these widgets, but they can also just leave it empty. If there are no widgets, I need to conditionally hide the entire sidebar.

The issue isn't getting the widgets to show on the screen, it's hiding the sidebar when there are no widgets selected. I've tried what feels like a million different snippets but nothing seems to work.

Anyone know how I can say something like "if this page field is empty"? Note: this needs to be done in a ASCX page template.

Correct Answer

Corey Megown answered on August 22, 2018 18:28

Thanks for your help Peter, but unfortunately neither of those snippets returned any results.

After some further digging it looks like adding items via the pages field type creates a relationship akin to "related pages" event though they aren't actually listed under Properties > Related Pages. I was still able to access the count of these pseudo related pages with this:

<%= CurrentDocument.RelatedDocuments.FirstOrDefault().Count() %>

0 votesVote for this answer Unmark Correct answer

Recent Answers


Peter Mogilnitski answered on August 22, 2018 03:14

not sure I understand, assuming you want to get value of the field "Pages" of you current document: Try to print it first:

<%=CMS.DocumentEngine.DocumentContext.CurrentDocument.GetValue("Pages")%>

or

<%# IfEmpty(CMS.DocumentEngine.DocumentContext.CurrentDocument.GetValue("Pages"), "Pages empty", "Pages has somehting") %>

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.