Get the droplink value in Code-Behind

I am trying to get the display name of an element in the sidebar in the internal C # code. I use Sitecore 6.6 without using MVC, and install a disconnect control in the CMS for clients named Address . The droplink source goes to /sitecore/Templates/User Defined/WAC/Address , and the individual elements have a SEO compatible name and readable display name.

For instance:

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

My code is:

 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 item id. If I use mainPage.Fields["Address"].DisplayName , I get "Address".

How can I get the display name of an element from a droplink?

+5
source share
4 answers

Use LookupField to get the refrence clause below - sample code:

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

If you want to use a single line, than use the code below:

 ((LookupField)mainPage.Fields["Address"]).TargetItem.DisplayName 
+6
source

Enter a value for the ReferenceField field. Then go to the TargetItem property:

 sl += "<span class=\"address\">" + ((ReferenceField)mainPage.Fields["Address"]).TargetItem.DisplayName +"</span>"; 
+2
source

Thomas answer will work based on your code. But I would suggest you also try using the ASP.Net and Sitecore server components.

This will avoid Null Reference errors, it will better support the Page Editor, and also simplify the work of your code.

You may have a repeater in your markup as follows:

 <asp:Repeater ID="rptAdresses" runat="server"> <ItemTemplate> <span class="address"> <sc:Text id="scAddress" runat="server" Field="__Display Name" Item="<%#(Sitecore.Data.Items.Item)Container.DataItem%>"></sc:Text> </span> </ItemTemplate> </asp:Repeater> 

And then bind the address for your code behind:

 private void BindRepeater() { if (mainPage.TemplateID.ToString() != "{27A9692F-AE94-4507-8714-5BBBE1DB88FC}") { rptAdresses.Visible = false; } else { rptAdresses.DataSource = Sitecore.Context.Item.GetChildren(); rptAdresses.DataBind(); } } 

Another thing I noticed is the line mainPage.TemplateID.ToString() != "{27A9692F-AE94-4507-8714-5BBBE1DB88FC}" . This is something you can also improve. Hard-coded identifiers are not a good estimate. You could create a class to hold these things, or better yet, you could think more about your design to avoid it.

amuses

+1
source

All other solutions are perfect, but you can also use the following code:

 var itemID=mainPage.Fields["Address"].value; Item targetItem=Sitecore.Context.Database.GetItem(itemID); if (mainPage.TemplateID.ToString() == "{27A9692F-AE94-4507-8714-5BBBE1DB88FC}") { sl += "<span class=\"address\">" + targetItem.DisplayName +"</span>"; } else { } 

With this approach, you can use each field of the target element.

0
source

Source: https://habr.com/ru/post/1212451/


All Articles