Search code examples
sitecoresitecore7

Sitecore how to show item's field in a datasource instead of item name


In a templateItem I have following information for a field.

Name : Product type
field type : 'Droplink' 
DataSource : DataSource=/sitecore/content/Enumerations/Products/Product type/

When the content editor creates an item based on above template, for the field 'Product type' in the dropdown he will see the items under ../Product type. My question is for the items which are show in dropdown how to show some other field instead of item name


Solution

  • This functionality does not exist out of the box, although the code for the DropLink field looks like has code in there to be able to do this (take a look at the GetItemHeader() method in Sitecore.Shell.Applications.ContentEditor.LookupEx), I do not know how to utilize the parameter through the Content Editor though...

    It's simple enough to create a custom field to achieve this though:

    using Sitecore;
    using Sitecore.Data.Items;
    using Sitecore.Shell.Applications.ContentEditor;
    
    namespace MyProject.Custom.Fields
    {
        public class CustomDropLink : LookupEx
        {
            protected override string GetItemHeader(Item item)
            {
                if (string.IsNullOrEmpty(this.FieldName))
                    this.FieldName = StringUtil.ExtractParameter("FieldName", this.Source).Trim();
    
                return base.GetItemHeader(item);
            }
        }
    }
    

    Then register your custom class in the core database under /sitecore/system/Field types/. You can do this by duplicating /sitecore/system/Field types/Link Types/Droplink and setting the following values:

    Assembly: MyProject.Custom
    Class: MyProject.Custom.Fields.CustomDropLink
    Control: <set this to empty>
    

    Then when you utilise this field set the Source of your field like so:

    Datasource=/sitecore/content/path/to/items&FieldName=Title