2

I'm wondering how could I modify my custom Treelist field to show, in the selected column, the path of the item instead of just the name. We have lots of duplicated names in different folders and this would make it much more understandable.

Thanks.

1 Answer 1

1

You might need to create a custom field type inheriting from TreeList. Refer to this official blog on how to create custom field types: https://www.sitecore.com/knowledge-center/blog/474/creating-a-custom-sitecore-field-4246

Then you need to do a bit of code work to have the custom treelist field to display items' paths instead of names:

public class PathTreeList : TreeList
{
    protected Listbox TreeListListBox
    {
        get
        {
            return typeof(TreeList).GetField("_listBox", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this) as Listbox;
        }
    }

    protected override void OnLoad(EventArgs args)
    {
        base.OnLoad(args);

        if (!Sitecore.Context.ClientPage.IsEvent)
        {
            foreach (var listItem in TreeListListBox.Items)
            {
                var id = listItem.Value.Split('|')[1];
                var item = Sitecore.Context.ContentDatabase.GetItem(id);
                if (item != null)
                {
                    listItem.Text = item.Paths.Path;
                }
            }
        }
    }
}

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.