GeneralLink in Sitecore

前端 未结 6 852
囚心锁ツ
囚心锁ツ 2020-12-03 12:26

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 standar

6条回答
  •  旧时难觅i
    2020-12-03 12:36

    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"];
    }
    

提交回复
热议问题