There will be times where you want to enable the Page Editor for a property on your model but the model itself may not have been mapped by Glass.Mapper.

The most obvious scenario for this is when using Sitecore search, you get search results back that are populated by Sitecore 7 search. The properties are filled with data from the Sitecore 7 search service but now you need to make them editable.

Lets look at the raw search code first, my model contains an ID property and a Title property, they will map the item's ID and Title field from the index respectively.

        public class MyResult 
        {
            [TypeConverter(typeof(IndexFieldIDValueConverter))]
            [IndexField("_group")]
            public ID Id { get; set; }
            
            [IndexField("title")]
            public string Title { get; set; }
        }

The code behind on my search ascx is shown below, notice that the results are coming directly from the search and are not going via Glass.Mapper, the user control also inherits from the AbstractGlassUserControl, this is just for convenience later.

    public partial class Search : AbstractGlassUserControl
    {

        public IEnumerable<MyResult>  Results { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            var index = ContentSearchManager.GetIndex("sitecore_master_index");
            using (var context = index.CreateSearchContext())
            {
                IQueryable<MyResult> searchItems = context.GetQueryable<MyResult>();
                Results = searchItems.Where(item => item.Title.Contains("V2")).ToList();
            }
        }
    }

Finally I render them to screen:

    <ul>
        <% foreach (var result in Results)
           { %>

        <li><%=result.Title %></li>

        <% } %>
    </ul>

Now we need to update the code above to support the Page Editor, since I have called my properties "ID" and "Title" then I can just rely on Glass' auto-mapping abilities, therefore the only change I need to make is to the HTML in my search page is to call the Editable method:

    <ul>
        <% foreach (var result in Results)
           { %>

        <li><%=Editable(result, x=>x.Title) %></li>

        <% } %>
    </ul>

Of course your property names may not always match the file names, in that case you can still use the Glass.Mapper attribute and fluent configuration:

        public class MyResult 
        {
            [TypeConverter(typeof(IndexFieldIDValueConverter))]
            [IndexField("_group")]
            public ID Id { get; set; }
            
            [IndexField("title")]
            public string Title { get; set; }
    
            [IndexField("computedvalue")]
            [SitecoreField("Text")]
            public string Computed {get;set;}
        }

Hopefully in this quick blog you have seen how easy it is to get the Page Editor working with any model using Glass.Mapper, even if that class hasn't been mapped by Glass.Mapper.

comments powered by Disqus