Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of droplink in Code-Behind

Tags:

c#

sitecore

I am trying to get the display name of an item in a droplink in the back-end C# code. I am using Sitecore 6.6, not using MVC, and am setting a droplink control in the CMS for clients called Address. The droplink source goes to /sitecore/Templates/User Defined/WAC/Address, and the individual items have an SEO-compliant name and a readable display name.

For example:

  • Item ID: {9E60F5F8-FBF2-4CBD-BB13-6A93397AAC87}
  • Name: 100-main-street
  • Display Name: 100 Main Street, Sample Town, 10011

My code:

protected void Page_Load(object sender, EventArgs e)
{
    String sl = "";
    Sitecore.Data.Items.Item currentItem = Sitecore.Context.Item;
    // BEGIN main class list
    Sitecore.Collections.ChildList classList = currentItem.Children;
    foreach (Sitecore.Data.Items.Item mainPage in classList)
    {
        if (mainPage.TemplateID.ToString() == "{27A9692F-AE94-4507-8714-5BBBE1DB88FC}")
        {
            sl += "<span class=\"address\">" + mainPage.Fields["Address"] +"</span>";
        }                            
        else
        {
        }
    }
    // END main class list
    classSessionList.Text = sl;
}

This code will give me the ID of the Item. If I use mainPage.Fields["Address"].DisplayName, I get "Address".

How can I get the Display Name of the item from the droplink?

like image 747
BaronForo Avatar asked Feb 02 '15 16:02

BaronForo


1 Answers

Use LookupField for getting reference item below are the sample code:

LookupField address= (LookupField)mainPage.Fields["Address"];
Item addressItem = address.TargetItem;
string displayName = addressItem.Fields["DisplayName"].Value;

If you want it in one line then use below code:

((LookupField)mainPage.Fields["Address"]).TargetItem.DisplayName
like image 67
Ashish Bansal Avatar answered Sep 24 '22 12:09

Ashish Bansal