Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GeneralLink in Sitecore

I'm new to Sitecore.. I have created a Page template and add a field for a URL of type General Link. I have created another field for the text for the link (this is standard practice in this project).

I simply want to display the link in my user control but I just cant get it to work. This should be simple but Im going round in circles

Here's an example of the code I've tried ..

ascx :

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

ascx.cs:

lnkMain.NavigateUrl = SiteCore.Context.Item.GetGeneralLink("Link1");
lnkMain.Text = item.GetFieldValue("Link1Text");
like image 546
Steve Ward Avatar asked Nov 12 '13 02:11

Steve Ward


2 Answers

You should be careful using linkField.Url since it it will incorrectly render internal links to Sitecore Items and Media. You should instead be using Sitecore.Links.LinkManager.GetItemUrl(item) and Sitecore.Resources.Media.MediaManager.GetMediaUrl(item) for those.

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 this Sitecore Links with LinkManager and MediaManager blog post which has the correct code 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;
    }
}

Usage:

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

It would be best of course to use <sc:FieldRender> control and let Sitecore handle it for you, but it looks like you do not have that option.

like image 131
jammykam Avatar answered Oct 11 '22 13:10

jammykam


As of 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 output a valid URL no matter what type of link the field contains. For internal links, the method returns a URL from LinkManager.GetItemUrl(). For media links, the method returns a URL from MediaManager.GetMediaUrl(). For external links, anchor links, e-mail links, and JavaScript links, the method returns the value of the LinkField.Url property. (400051)

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

like image 36
KochFolie Avatar answered Oct 11 '22 14:10

KochFolie