1

There is a requirement that came across in one of our project, where we want to display items in sort order in the Multilist field. OOTB functionality of multilist field to show items based on camel case order. But our requirement is that we want to order it based on Alphabetic order. Is there any clue?

enter image description here

2 Answers 2

1

This is not OOTB available, you can achieve this with override multilistex.

namespace Company.Configuration.Component{

public class CustomMultilist : Sitecore.Shell.Applications.ContentEditor.MultilistEx
{
    protected override void GetSelectedItems(Item[] sources, out ArrayList selected, out IDictionary unselected)
    {
        base.GetSelectedItems(sources, out selected, out unselected);
        var resorted = new SortedList<string, Item>(new DescendingComparer());
        foreach (DictionaryEntry entry in unselected)
        {
            var item = (Item)entry.Value;
            var sortKey = MainUtil.GetSortKey(item[global::Sitecore.FieldIDs.Updated]);
            resorted.Add(sortKey, item);
        }
        unselected = resorted;
    }

    protected class DescendingComparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            return Comparer<string>.Default.Compare(x, y);
        }
    }
}}

create a new field type in the core database and update config for the custom field.

0

This is not OOTB behaviour in Sitecore I think but you can achieve this by creating a custom field.

Follow the blog - https://aldrichdev.wordpress.com/2015/12/01/how-to-alphabetize-the-selected-list-of-a-field-in-sitecore/

Below implementation is for Treelist but you can modify it for your field type.

  1. The first thing you will need to do is create a new class for your custom list field. Ideally you would place this in the Fields folder of your project, if you have one. Edit the class so that it inherits from the appropriate base class, i.e. TreeList.
  2. Then, create a new custom field in Sitecore and publish it. For instructions on how to do that, see my previous blog (link here). In fact, if you already went through the instructions of that post, this is already done.
  3. Going back to your class created in Step 1, you’ll want to override the OnLoad() event and add in the logic to alphabetize. My method was (pseudo code):

i) Get the current list of guids in the field.

ii) Convert each to an item and add it to a generic list.

iii) Use a generic OrderBy() to sort the list by item display name.

iv) Build a string of pipe-delimited, guids from the sorted list, and set the list field’s value to that.

The result was the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Sitecore;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Resources;
using Sitecore.SecurityModel;
using Sitecore.Shell.Applications.ContentEditor;
using Sitecore.Web.UI.HtmlControls;
using Sitecore.Web.UI.Sheer;
using Sitecore.Web.UI.WebControls;

namespace your.namespace.here
{
    public class AlphaTreeList : TreeList
    {
        protected override void OnLoad(EventArgs args)
        {
            if (!string.IsNullOrEmpty(base.ItemID))
            {
                SortAssociatedProductsAlpha(Sitecore.Context.ContentDatabase.GetItem(base.ItemID));
            }

            base.OnLoad(args);
        }

        private void SortAssociatedProductsAlpha(Item i)
        {
            using (new SecurityDisabler())
            {
                var associatedProducts = i.Fields["Associated Products"];
                var alphaGuids = GetAlphabetizedGuidString(i, associatedProducts);

                if (alphaGuids.Equals(associatedProducts.Value)) return;

                if (alphaGuids != string.Empty)
                {
                    using (new EditContext(i))
                    {
                        this.Value = alphaGuids;
                    }
                }
            }
        }

        private string GetAlphabetizedGuidString(Item i, Field f)
        {
            List<Item> items = new List<Item>();
            StringBuilder scGuidBuilder = new StringBuilder();

            if (i != null && f != null)
            {
                foreach (ID guid in ((MultilistField)f).TargetIDs)
                {
                    Item target = Sitecore.Data.Database.GetDatabase("master").Items.GetItem(guid);
                    items.Add(target);
                }

                // Sort it by item name.
                items = items.OrderBy(o => o.DisplayName, StringComparer.OrdinalIgnoreCase).ToList();

                // Build a string of pipe-delimited guids.
                foreach (Item item in items)
                {
                    scGuidBuilder.Append(item.ID);
                    scGuidBuilder.Append("|");
                }

                // Return string which is a list of guids. And remove that last pipe.
                return scGuidBuilder.ToString().TrimEnd('|');
            }

            return string.Empty;
        }
    }
}

Lastly, you should change the field type for this field in your environment to your new custom field type.

This should work every time you open the item or add a new item to the field’s Selected list and save.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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