I have a requirement where I need to display "Title" field value of the items in the DropLink field instead of the item display names. How do I do it?
2 Answers
1) Create a custom drop link field in the core database. In a newly created field, make control field empty.
2) Include the following code in your project and give the assembly name and class name of the following code in a newly created custom drop link field item.
public class FieldDropLink : Sitecore.Shell.Applications.ContentEditor.LookupEx
{
protected override string GetItemHeader(Sitecore.Data.Items.Item item)
{
if (string.IsNullOrEmpty(this.FieldName)
|| item[this.FieldName].StartsWith("@",StringComparison.OrdinalIgnoreCase)) // don't impact default usage
{
return base.GetItemHeader(item);
}
if (string.IsNullOrEmpty(item[this.FieldName]))
{
return item.DisplayName;
}
return item[this.FieldName];
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (String.IsNullOrEmpty(this.Source)
|| !this.Source.Contains("="))
{
return;
}
NameValueCollection args = Sitecore.Web.WebUtil.ParseUrlParameters(
this.Source);
if (!String.IsNullOrEmpty(args["field"]))
{
this.FieldName = args["field"];
}
this.Source = args["source"] == null ? String.Empty : args["source"];
}
}
3) Select newly created drop link in your template and give your datasource followed by &field=yourfiledname
For example: source=/sitecore/content/home&field=title
You can do this, but you would then change the behaviour of all your DropLink fields.
Switch to the "core" database and navigate to /sitecore/system/Field types/Link Types/Droplink
Add FieldName=Title
to Parameters
.
If you don't want this to be the default for ALL your Droplink fields, I suggest you make a Duplicate of this field type and update your templates as required.
Additional reading: https://community.sitecore.net/technical_blogs/b/sitecorejohn_blog/posts/display-a-field-value-in-a-droplink-with-the-sitecore-asp-net-cms