0

I'm trying to grab the id and/or url attribute of a general link rendering parameter in the view, but I can't seem to make it work. I can't cast it to a Linkfield (it says it can't cast a string to a linkfield). Is the only way to do this to manually parse through the link tag that is generated as the output for this general link in the view?

@using Sitecore.Mvc.Presentation
@using Sitecore.Mvc
@using Sitecore.Data.Fields;

 @model RenderingModel


<h2>@RenderingContext.Current.Rendering.Parameters["Title"]"></h2>
<p>@RenderingContext.Current.Rendering.Parameters["Description"]"></p>
@{
var link = ((LinkField)@RenderingContext.Current.Rendering.Parameters["Feed or File Link"]).GetFriendlyUrl();
}
<p>@link</p>
1
  • Please add code snippet to your question May 24, 2019 at 19:54

2 Answers 2

1

First of all, I don't think Rendering Parameters are right place for a General Link field. They cannot be localized, so if your content authors will set some text of the link, it will be the same for all the languages.

You may try to use field like Droplink if you don't need a title of your link and if you always want to select Sitecore item. Or maybe moving the field to datasource is an option?

But if you really really want to parse Link Field value without using LinkField class, find the sample code below. Be aware that this code is tested on 1 value only, so you may need to adapt it to your needs:

public class LinkFieldParser
{
    private XmlDocument _xmlDocument;

    public LinkFieldParser(string linkFieldValue)
    {
        if (!string.IsNullOrEmpty(linkFieldValue))
        {
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(linkFieldValue);
                _xmlDocument = xmlDocument;
            }
            catch (Exception ex)
            {
                Log.Error($"Attempted to load invalid xml: '{linkFieldValue}'", ex, this);
            }
        }
    }

    public string Url
    {
        get
        {
            if (_xmlDocument == null)
                return null;

            switch (GetAttribute("linktype"))
            {
                case "internal":
                    var item = Sitecore.Context.Database.GetItem(GetAttribute("id"));
                    return item != null ? LinkManager.GetItemUrl(item) : string.Empty;
                case "media":
                    var mediaItem = Sitecore.Context.Database.GetItem(GetAttribute("id"));
                    return mediaItem != null ? MediaManager.GetMediaUrl(mediaItem) : string.Empty;
                case "anchor":
                    var anchor = GetAttribute("anchor");
                    return !string.IsNullOrEmpty(anchor) ? "#" + anchor : string.Empty;
                default:
                    return GetAttribute("url");
            }
        }
    }

    public string GetAttribute(string name)
    {
        return _xmlDocument?.DocumentElement?.Attributes[name].Value.ToLower();
    }
}

and in your scenario you would use

@(new LinkFieldParser(RenderingContext.Current.Rendering.Parameters["Feed or File Link"]).Url)
0

//you can use any other method to get item ex- database.getitem()

var item = _context.GetRenderingDatasource< ICircleTeaserItem>();

//use below code to render the link - Button is my field name

string linkCssClass = "link-arrow teaser-link-arrow";

var link = new HtmlString(FieldRenderer.Render(item.InnerItem, "Button", $"class={linkCssClass}"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.