sitecoresitecore8glass-mapper

Sitecore GlassMapper MVC add standard fields to model


I need the sort order of my item in a specific model.

I have tried it like this but no luck:

[SitecoreField("__Sortorder")]
public string SortOrder { get; set; }

Any idea how I can include standard fields in my model?

If this is not possible, actually I am working with a Checklist in that case, The AutoMapping from GlassMapper does return it in alphabetical order.

Here is the Definition of the Checklist Field:

IEnumerable<Guid> Filter_Tags_To_Shows  {get; set;}

And like mentioned before, the list is returned in alphabetical order but I have no change to sort afterwards since missing the sortOrder field.

Thanks in Advance.


Solution

  • Your Filter_Tags_To_Shows property is returning a list of Guids which, I presume, you are then doing something to get the linked Item from Sitecore. This is not the most efficient way and Glass allows you to specify the linked Item type which it will them automap.

    IEnumerable<FilterTagClass> Filter_Tags_To_Shows  {get; set;}
    

    Assuming that the FilterTagClass has the sort order specified like in your question:

    [SitecoreType(TemplateId={GUID})]
    public class FilterTagClass : GlassBase 
    {
        [SitecoreField("__Sortorder")]
        public virtual int SortOrder { get; set; }
    }
    

    You can sort using Linq:

    @foreach (var tag in Filter_Tags_To_Shows.OrderBy(i => i.SortOrder))
    {
        // do stuff
    }
    

    This is a re-hash of the answer from @DougCouto but note it is not necessary to convert IDs to object if you set up the properties and mapping correctly.

    As an alternative to using the Checklist field you can use the Multilist field, which will allow you select tags and re-order them as required. Glass should return them in the same order you have set then.