GeneralLink in Sitecore

吃可爱长大的小学妹 提交于 2019-11-27 14:28:06

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.

KochFolie

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/

It'd 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>

That way, you don't have to do any code-behind stuff and you'll be able to use the Page Editor as well.

Yogesh Sharma

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.

When you assign a value to a GeneralLink field of an item there is a field labeled "Link Description" in the Internal Link dialog that pops up. Fill in that value then use:

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

That's it. Everything is "wired-up" for you, auto-magically.

HiralShah

You need to get the Linkfield value of the item and than get the LinkField type of that item. This will give you the type of link either an "Internal", "external", "mailto" and based on that can get the url of the link field as this is mentioned by @jammykam.

Same thing you can do to retrieve the LinkText as well.

For 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"];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!