This project is read-only.

How can I display a message when the query returns no records for a projection?

Topics: Core, Customizing Orchard
Apr 8, 2013 at 5:02 PM
Using the admin panel on Orchard CMS I've created the following:

A content type called CalendarEvent, it contains several fields including the EventDate;
A query that has 2 filters, one by the content type (= CalendarEvent) and another one based on the date of the event. The Display Mode on the Layout is set to Properties;
A projection to display the query when a menu item is clicked.
The problem is that based on the EventDate we only display upcoming events, not the ones in the past. If for some reason there are no events to be displayed, all the user gets is an empty page with no information whatsoever.

My question is, how can I modify my query or projection in order to display something like: "There are no current events scheduled"?

I know the Properties on the Query's Layout allow me to specify a "No Result", but this implies that a record is present and that the actual property is empty. However, in my example, no record is present.

Thank you all in advance.


By the way, I am using the latest Orchard version 1.6.
Apr 10, 2013 at 10:46 AM
Easiest way I found to do it is using Contrib.ProjectionLayouts, which adds two additional Layouts: Shape and Raw. When using the Shape layout, you take over rendering of the list completely (you have to iterate over the content items or shapes yourself). In this template, you can easily check against the number of items. If 0, render your "No Results", message. Otherwise, render the list of items.
Apr 12, 2013 at 5:52 PM
Edited Apr 12, 2013 at 7:34 PM
Do you know where I can find more documentation on how to do this?

By the way, I followed the documentation here, and created the Shape Layout and called it "abcdef" for example, then under my Theme > Views, I created a file called abcdef.cshtml, but it doesn't get hit when I run the project.
Thanks for your help.
Apr 12, 2013 at 11:41 PM
The documentation you mentioned is all I know of.
From what you're saying you did everything right. Just make sure that the projection is configured to use the correct query + layout.
Apr 25, 2013 at 1:58 PM

I am going to provide a few screenshots. Maybe Sebastien or someone else will take a look and see right away what I am doing wrong.

Step1: Module Installation

Step 2: Query Setup


When previewing the query, we see that there is data to be displayed...

Step 3: Layout Setup (Shape Layout)

Step 4: Projection Setup

When clicking on the menu, I get the following:

Adding the View:

View's Source Code:

Notice that I get the same result as before when I didn't have a view...

Again, thanks for your help!
Apr 25, 2013 at 6:14 PM
No, description is not the shape name. The shape type is where you should put it.
Apr 26, 2013 at 2:37 PM
Bertrand, thank you for your feedback. By matching the Shape Type to the View Name, I now get a display.

Do you know how can I access the fields/values on the View so that I can better customized, instead of a plain list?

Apr 26, 2013 at 2:52 PM
I figured it out. Thanks!
@using Orchard.ContentManagement
    var buildShapes = Model.BuildShapes;

<div class="row">
    @foreach (dynamic shape in buildShapes()) {        
        dynamic eventInstance = shape.ContentItem.TestProjectionLayoutContentType;