Sitecore GlassMapper MVC add standard fields to model

955 views Asked by At

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.

3

There are 3 answers

0
jammykam On BEST ANSWER

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.

1
DougCouto On

This property

IEnumerable<Guid> Filter_Tags_To_Shows  {get; set;}

will give you a list of IDs for the items that you selected in the checklist field. To get the sortorder of the actual items, you need to convert these IDs into objects:

var tagsToShow = Filter_Tags_To_Shows.Select(i => SitecoreContext.GetItem<SomeClassType>(i));

Assuming SomeClassType has the SortOrder field, you should then be able to access that field for each object in tagsToShow and do something like this:

var sortedTagsToShow = tagsToShow.OrderBy(i => i.Sortorder);
0
Jitendra soni On

Checklist and Multilist are stored as raw text in the Sitecore database and the sequence will be updated based on data source item sequences, You can try the sample below.

Sitecore.Data.Fields.MultilistField multilistField = item.Fields["FieldName"];

foreach (var child in multilistField.GetItems)
{
    // It should provide in short order
}