Generallink at Sitecore

I am new to Sitecore. I created a page template and add a field for URLs like General Link. I created another text box for the link (this is standard practice in this project).

I just want to display the link in my user control, but I just can't get it to work. It should be easy, but Im spinning in circles

Here is an example of the code I tried ..

ascx:

<asp:HyperLink runat="server" ID="lnkMain"></asp:HyperLink> 

ascx.cs:

 lnkMain.NavigateUrl = SiteCore.Context.Item.GetGeneralLink("Link1"); lnkMain.Text = item.GetFieldValue("Link1Text"); 
+8
c # sitecore
source share
6 answers

You should be careful when using linkField.Url , as it will not display internal links to Sitecore and Media elements correctly. Instead, you should use Sitecore.Links.LinkManager.GetItemUrl(item) and Sitecore.Resources.Media.MediaManager.GetMediaUrl(item) for them.

It would be better to have a helper (extension) method to return the correct URL for you based on the type of link. Take a look at this post "Sitecore Links with LinkManager and MediaManager ), which has the correct code that you need for this.

For reference:

 public static String LinkUrl(this Sitecore.Data.Fields.LinkField lf) { switch (lf.LinkType.ToLower()) {  case "internal":    // Use LinkMananger for internal links, if link is not empty    return lf.TargetItem != null ? Sitecore.Links.LinkManager.GetItemUrl(lf.TargetItem) : string.Empty;  case "media":    // Use MediaManager for media links, if link is not empty    return lf.TargetItem != null ? Sitecore.Resources.Media.MediaManager.GetMediaUrl(lf.TargetItem) : string.Empty;  case "external":    // Just return external links    return lf.Url;  case "anchor":    // Prefix anchor link with # if link if not empty    return !string.IsNullOrEmpty(lf.Anchor) ? "#" + lf.Anchor : string.Empty;  case "mailto":    // Just return mailto link    return lf.Url;  case "javascript":    // Just return javascript    return lf.Url;  default:    // Just please the compiler, this    // condition will never be met    return lf.Url; } } 

Using:

 Sitecore.Data.Fields.LinkField linkField = item.Fields["Link1"]; lnkMain.NavigateUrl = linkField.LinkUrl(); 

It would be best to use the <sc:FieldRender> and let Sitecore handle it for you, but it looks like you don't have this option.

+28
source share

As in Sitecore 7.2, there is an alternative to linkField.Url:

 Sitecore.Data.Fields.LinkField linkField = item.Fields["Link1"]; lnkMain.NavigateUrl = linkfield.GetFriendlyUrl(); 

A new LinkField.GetFriendlyUrl () method has been introduced. The method makes it easy to display a valid URL no matter what type of link the field contains. For internal links, the method returns the URL from LinkManager.GetItemUrl (). For media links, the method returns the URL from MediaManager.GetMediaUrl (). For external links, anchor links, email links, and JavaScript links, the method returns the value of the LinkField.Url property. (400051)

http://techitpro.com/uncategorized/sitecore-7-2-changes/

+10
source share

It would be easier if you use the Link control:

 <sc:Link Field="Link1" runat="server" ID="link"> <sc:Text Field="Link1Text" runat="server" ID="linkText" /> </sc:Link> 

This way you do not need to do any code elements, and you can also use the Page Editor.

+5
source share

You can use below

 <sc:Link ID="scLink" runat="server" Field="Your Link Field Name"> <sc:FieldRenderer ID="frTest" runat="server" FieldName="Your Text Field Name" /> </sc:Link> 

It will work for you.

+2
source share

You need to get the Linkfield value of the element and get the Linkfield type of this element. This will give you the type of link, either “Internal”, “External”, “mailto” and based on this can get the URL of the link field, as indicated by @jammykam.

The same can be done for LinkText recovery.

Reference:

 public static string GetGeneralLinkText(LinkField link) { text = ""; if (link == null) return false; if (!string.IsNullOrEmpty(link.Text)) { text = link.Text; return true; } switch (link.LinkType) { case "internal": if (link.TargetItem == null) return false; text = link["Text Field Name"]; break; case "external": case "mailto": case "anchor": case "javascript": text = link.Text; break; case "media": if (link.TargetItem == null) return false; Sitecore.Data.Items.MediaItem media = new Sitecore.Data.Items.MediaItem(link.TargetItem); text = media.Name; break; case "": break; default: return ""; } return link["Text Field Name"]; } 
+1
source share

When you assign a value to the GeneralLink field of an element, a field labeled “Link Description” appears in the Internal Link dialog box. Fill this value, then use:

 <sc:Link runat="server" Field="{YourFieldName}" /> 

What is it. Everything is “connected” for you, automatically magically.

+1
source share

All Articles