2

I have custom RTE field in Sitecore form where I have used TextViewModel and the custom view below:

@model Sitecore.ExperienceForms.Mvc.Models.Fields.TextViewModel

<@Model.HtmlTag data-sc-field-key="@Model.ConditionSettings.FieldKey" class="@Model.CssClass">@Html.Raw(Model.Text)/@Model.HtmlTag

When trying to add an internal link in the RTE field its not showing a friendly url it's showing link id format.

How to fix this?

2 Answers 2

1

Here is how you can fix this:

1.Ensure the link provider is correctly configured: The link provider should resolve the internal link GUIDs to friendly URLs. Ensure that your LinkProvider settings in the Sitecore.config or Web.config are correctly configured.

2.Use the RenderField helper: Instead of directly outputting the Model.Text, you should use the @Html.Sitecore().Field() helper, which correctly resolves links and other dynamic content within RTE fields.

 @model Sitecore.ExperienceForms.Mvc.Models.Fields.TextViewModel

<@Model.HtmlTag data-sc-field-key="@Model.ConditionSettings.FieldKey" class="@Model.CssClass">@Html.Sitecore().Field("Text", Model.Item)/@Model.HtmlTag

This helper method renders the content of the field named "Text" from the Model.Item. The Field helper will process internal links, media links, and other dynamic content properly, converting link GUIDs to friendly URLs.

hope this helps!

If you don't have Model.item , You can try this code as well to resolve your internal code

@model Sitecore.ExperienceForms.Mvc.Models.Fields.TextViewModel
@using Sitecore.Links
@using System.Text.RegularExpressions

@functions {
    // Function to resolve internal Sitecore links to friendly URLs
    public string ResolveInternalLinks(string rawHtml)
    {
        if (string.IsNullOrEmpty(rawHtml))
        {
            return rawHtml;
        }

        // Pattern to find Sitecore internal links in the format sitecore://{GUID}
        var linkPattern = new Regex(@"href=[""']sitecore://([^""']+)[""']", RegexOptions.IgnoreCase);
        return linkPattern.Replace(rawHtml, match =>
        {
            var linkId = match.Groups[1].Value; // Extract the GUID from the match
            var item = Sitecore.Context.Database.GetItem(linkId); // Get the item using the GUID
            if (item != null)
            {
                var url = LinkManager.GetItemUrl(item); // Resolve the item to a friendly URL
                return $"href=\"{url}\""; // Replace the link with the friendly URL
            }
            return match.Value; // Return the original match if the item is not found
        });
    }
}

@{
    // Use the helper function to resolve links in the Model.Text
    var resolvedText = ResolveInternalLinks(Model.Text);
}

<@Model.HtmlTag data-sc-field-key="@Model.ConditionSettings.FieldKey" class="@Model.CssClass">
    @Html.Raw(resolvedText)
</@Model.HtmlTag>

Hope this helps!

2
  • Model.Item will not be there in TextViewModel
    – Arun
    Commented May 20 at 11:51
  • @Arun I have edited and updated new code you can try this . This will resolve your issue Commented May 20 at 13:00
0

You can use LinkManager ExpandDynamicLinks helper method as below, to transform internal links on the rich text field.

@using Sitecore.Links

<@Model.HtmlTag data-sc-field-key="@Model.ConditionSettings.FieldKey" class="@Model.CssClass">
   @Html.Raw(LinkManager.ExpandDynamicLinks(Model.Text)) 
</@Model.HtmlTag>

Using above code ~link.aspx format in rich text field will be resolved to a friendly url.

Note: When you browse the rich text field in Content Editor inside Sitecore CMS, links will stored in link.aspx format. But when you browse the same in standalone or CD server it will be resolved to friendly url. Pipeline responsible for transforming this is as below.

Sitecore.Pipelines.RenderField.ExpandLinks, Sitecore.Kernel

Hope this helps!!!

3
  • Not working still same link format.
    – Arun
    Commented May 20 at 11:54
  • @Arun Hope you have cleared the cache as well..Can you also share the what is the exact value you are getting on Model.Text variable
    – ckhanna
    Commented May 20 at 12:06
  • Also note the links will be stored as link.aspx format inside Content Editor. But when you browse the pages on standalone or CD server, they will be transformed to friendly urls.
    – ckhanna
    Commented May 20 at 13:18

Your Answer

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

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