Using Queries

This tutorial will show you how you can enhance you models using queries.

We need a page that lists all the news articles in our news section to this we will create a news landing page. I have changed the news section to store stories in a year/month structure:


We will use a query to select only the news articles and ignore the folders. The model for the news landing page looks like this:

using System.Collections.Generic;
using Glass.Mapper.Sc.Configuration.Attributes;
using Glass.Mapper.Sites.Sc.Models.Content;
using Glass.Mapper.Sites.Sc.Models.Misc;

namespace Glass.Mapper.Sites.Sc.Models.Landing
{
    [SitecoreType(AutoMap = true)]
    public class NewsLanding : ContentBase
    {
        [SitecoreQuery("./*/*/*[@@templatename='NewsArticle']", IsRelative = true)]
        public virtual IEnumerable<NewsArticle> Articles { get; set; }
    }
}

The query is standard Sitecore query, the IsRelative at the end is important, it tells Glass.Mapper to execute the query relative to the item the class represents, i.e. the news landing page item. If we set IsRelative to false then Glass.Mapper will query from the root of the Sitecore. Queries are executed lazily and are therefore only populated when the property is accessed for the first time.

When using a query the target property can be a generic IEnumerable as in our example or a single class. If a single class is used then the first item returned by the query is mapped.

Now we just need to create our landing page markup, the code behind is below:

using Glass.Mapper.Sc.Web.Ui;
using Glass.Mapper.Sites.Sc.Models.Landing;

namespace Glass.Mapper.Sites.Sc.layouts.Site.Landing
{
    public partial class NewsLandingSublayout : GlassUserControl<NewsLanding>
    {
    }
}


And the the ASCX:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="NewsLandingSublayout.ascx.cs" 
    Inherits="Glass.Mapper.Sites.Sc.layouts.Site.Landing.NewsLandingSublayout" %>
<div class="page-header">
    <h1><%=Editable(x=> x.Title) %> <small></small></h1>
</div>
<ul class="thumbnails">
    <% foreach (var article in Model.Articles.OrderBy(x=>x.Date))
       { %>
    <li>
        <div class="span1"></div>
        <div class="span3">
            <a title="<%=article.Title %>" class="thumbnail">
                <img src="<%=article.FeaturedImage.Src%>"  />
            </a>
        </div>
        <div class="span7">
            <div class="row">
                <div class="span5">
                    <h3><a href="<%=article.Url %>"><%=article.Title %></a></h3>
                </div>
                <div class="span2">
                    <p><%=article.Date.ToString("dd MMM yyyy") %></p>
                </div>
            </div>
            <div class="row">
                <p  class="span7">
                    <%=article.Abstract %>
                </p>
            </div>
            <div class="row">
                <div class="span7">
                    <a title="<%=article.Title %>" class="pull-right">Read More</a>
                </div>
            </div>

        </div>
        <div class="span1"></div>
    </li>
    <% } %>
</ul>

If we compile and navigate to the landing page we should see the following result:


This tutorial has shown you how you can use queries on your models to load items from across you Sitecore solution. 

A word of warning, queries can be very useful but they may not be appropriate in every situation. Sometimes it maybe better to use Sitecore's search facility to find items instead.