GeneralLink in Sitecore

2020-01-29 08:14发布

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");

6条回答
SAY GOODBYE
2楼-- · 2020-01-29 08:18

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.

查看更多
女痞
3楼-- · 2020-01-29 08:32

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/

查看更多
▲ chillily
4楼-- · 2020-01-29 08:32

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"];
}
查看更多
Fickle 薄情
5楼-- · 2020-01-29 08:39

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.

查看更多
Lonely孤独者°
6楼-- · 2020-01-29 08:39

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.

查看更多
来,给爷笑一个
7楼-- · 2020-01-29 08:45

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.

查看更多
登录 后发表回答