2

I have a rendering parameter template with a generic link field, now I want to get the friendly url (No glass mapper in this project), is there a way to convert rendering parameters to fields based on the template?

2

Well there's almost two questions there. One is how to get a friendly Url from a General Link Field. This will first require you to determine if it's an internal link and therefore there is an ID for the item they link to, or if it's external, that should already have a friendly url.

Keep in mind the General Link field stores everything in Xml, so you'll need to parse this xml to determine the link type.

If it's an internal Url and therefore ID, you can get the Item by it's ID and then pass it into the Link Manager to get it's friendly url based on Link Manager settings for your site.

ID itemId = new ID("{EB18D6BC-4264-42B0-9480-21C10BA13706}");
Sitecore.Links.UrlOptions urlOptions = Sitecore.Links.UrlOptions.DefaultOptions;
string friendlyUrl = string.Empty;

if (itemId != null)
{
    Item urlItem = Sitecore.Context.Database.GetItem(itemId);

    if (urlItem != null) 
    {
        friendlyUrl = Sitecore.Links.LinkManager.GetItemUrl(urlItem, urlOptions);
    }
}

So you would want to get the id from the xml, whereas I just show how to get the Url using the LinkManager once you have an id above.

If you wanted to build that into a class that represents the Rendering Parameter Template, that's fairly straight forward as well, just create a Property on your class called Url or something like that, and inside that Property declaration you can perform the logic I mention above.

1
  • Thanks Dylan, Although I was hoping to find a way to map the parameters to an item based on the parameters template so I can work with them as normal! Seems there is no solution like that and I have to deal with fields differently! – Reza Fard May 3 '17 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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