1

I am not able to get glass to return items sorted correctly.

I have this code.

var options = new GetItemsByQueryOptions(new Query($"<mypath>/*"));
var myitems= this.contentRepository.GetItems<IMyType>(options)?.ToList();

It returns the correct items but not in the order they are sorted in the tree.

This code returns the items sorted as expected.

Sitecore.Context.Database.GetItems("mypath").ToList()

I found a post on here where it was said Glass returns the same order the Sitecore API does. The below API call does not return sorted so I assume Mike was talking about the above what which leaves me confused by Glass is not returning them sorted.

Sitecore.Context.Database.SelectItems("myquery").ToList()

I am on Glass 5.4

1

There is no such a thing like "correct order or items returned by Sitecore query".

Only children of an item are sorted.

Sort order information is stored on the parent item and if you use SitecoreChildren with Glass, you will get them in the order from Sitecore Content Tree, e.g.:

[SitecoreChildren(IsLazy = false)]
public virtual IEnumerable<Child> Children { get; set; }
| improve this answer | |
  • To phrase that another way, this means there is no way to get a sorted list from Glass using any of the GetItems methods. Your statement makes sense but I am just confused by a previous post (really wish I could find it again) where Mike said it should. But since I can't find that post again it is probably a misunderstanding. Just thought Glass would have a way to get children of a certain type in order. Sounds like I just need to change how we are getting these items and use more of a child model. – ToddB Oct 2 '19 at 15:10
  • Why don't you get the parent item itself using Glass and use Glass SitecoreChildren on that parent item Children property? – Marek Musielak Oct 2 '19 at 18:17
  • Looking into that. The challenge is I have multiple types that could have these as children so I have to setup the Child map on all those. It also makes the rendering less reusable as content authors can't just add this child items and put renderings on the screen. The other option is try and try get __sortOrder mapped to the Glass item but can't seem to get that field to map. – ToddB Oct 2 '19 at 18:28
  • @ToddB create a base class & template that all the parent items that can have the same children inherit. Then in glass get the parent items based on the base class which uses SitecoreChildren to get all the different types of children. That is how I've tackled it in the past. – Anicho Oct 4 '19 at 13:34
  • We are using interfaces with Glass so the base child item does work but Glass does not let you do InferType with Interfaces so it still causes issues because we can't get to the real type. I solved this by adding __sortOrder to the base interface object so now when a collection is returned we can sort it by the value of that field. – ToddB Oct 4 '19 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.