More in this section
Categories
Bloggers
Blogs RSS feed

Dynamically resolving Sitefinity links when using HtmlField

by User Not Found

This is a solution to dynamically resolving links for sites running version of Sitefinity lower than 5.4 sites. Sitefinity 5.4 provides this out of the box.

When using HtmlField in user control or by adding custom fields of type LongText to Sitefinity modules and selecting pages, images, documents or videos using the built in selectors for those the link to the selected content or page Sitefinity will generate:

<a href="/page" SFREF="[F669D9A7-009D-4D83-DDAA-000000000002]BDC53495-7357-4B44-82D9-572C1A71374E">Page</a>
 the link indicates the selected page url is ~/page and the other attribute added to the link "SFREF" is used to resolve the proper link to the page selected if its moved to another url.

For example if the page is moved under another page its url will be ~/parent/page

When selecting pages or media content in Sitefinity built in ContentBlock widget which used HtmlField the link is resolved automatically if the page or image has been moved to new location or their url was changed.

 If you use HtmlField or LongText field in user control or as custom field in module you will not get the automatic link resolution.

To fix this when rendering the html entered in the HtmlField use use Sitefinity LinkParser class

//get the html from HtmlField and convert it in format usable for ResolveLinks method
var fixedHtml = LinkParser.UnresolveLinks(Name);
 
//resolve the links if there has been a change in the url
            NameLiteral.Text = LinkParser.ResolveLinks(fixedHtml, DynamicLinksParser.GetContentUrl, null, false);

 Attached is a user control utilizing html and implementing the use of LinkParser.

 

When using LongText field as custom field to modules the selected media or pages links also will not resolve and to overcome this, create a custom htmlField (the long text field is htmlfield control), which inherits from the default one and override GetStringValue method to use LinkParser

public class HtmlFieldCustom : HtmlField
  
{
  
    protected override string ScriptDescriptorType
  
    {
        get
        {
            return typeof(HtmlField).FullName;
        }
  
    }
  
    public override string GetStringValue(object value)
    {
        var val = base.GetStringValue(value);
  
        var fixedHtml = LinkParser.UnresolveLinks(val);
  
        val = LinkParser.ResolveLinks(fixedHtml, DynamicLinksParser.GetContentUrl, null, false);
  
        return val;
    }
}
 If you have added the field to News module (for other modules the approach is the same) edit the module widget template and in the template register the namespace of the class created above and use the class to render the LongText field.

<%@ Control Language="C#" %>
  <%@ Register TagPrefix="sf" Namespace="SitefinityWebApp" Assembly="SitefinityWebApp" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.PublicControls.BrowseAndEdit" Assembly="Telerik.Sitefinity" %>
  
<telerik:RadListView id="listsControl" runat="server"
                     ItemPlaceholderId="ListContainer"
                     EnableEmbeddedSkins="false"
                     EnableEmbeddedBaseStylesheet="false">
  
    <LayoutTemplate>
        <div class="sfexpandableListWrp">
            <div class="sflistExpandAllLnkWrp">
                <asp:LinkButton ID="listExpandAllLnk" runat="server" class="sflistExpandAllLnk" Text="<%$ Resources:Labels, ExpandAll %>" OnClientClick="return false;" />
                <asp:LinkButton ID="listCollapseAllLnk" runat="server" class="sflistCollapseAllLnk" style="display: none;" Text="<%$ Resources:Labels, CollapseAll %>" OnClientClick="return false;" />
            </div>
            <asp:PlaceHolder id="ListContainer" runat="server" />
  
        </div>
    </LayoutTemplate>
    <ItemTemplate>
  
            <h2 class="sflistTitle">
  
                <asp:Literal runat="server" Text='<%# Eval("Title") %>' />
  
            </h2>
  
            <telerik:RadListView ID="listItemsControl" runat="server"
  
                    ItemPlaceholderID="ItemsContainer"
   
                    EnableEmbeddedSkins="false"
  
                    EnableEmbeddedBaseStylesheet="false">
  
                <LayoutTemplate>
  
                    <ul class="sflistList">
  
                        <asp:PlaceHolder ID="ItemsContainer" runat="server" />
  
                    </ul>
  
                </LayoutTemplate>
  
                <ItemTemplate>
  
                    <li class="sflistListItem">
  
                        <h3 class="sflistItemTitle">
  
                            <asp:LinkButton runat="server" ID="listItemToggleLnk" class="sflistItemToggleLnk" Text='<%# Eval("Title") %>' OnClientClick="return false;" />
  
                        </h3>
  
                        <div runat="server" ID="listItemContent" class="sflistItemContent" style="display: none;">
  
                            <asp:Literal runat="server" Text='<%# Eval("Content") %>' />
                          <sf:HtmlFieldCustom runat="server" DisplayMode="Read" Value='<%# Eval("TestField")%>' />
                        </div>
 

8 comments

Leave a comment
  1. Nidhi Feb 15, 2013

    I was having this issue in custom widgets where I used the HTMLField and this fixed it. Thank you!

  2. Stephen Mar 03, 2013
    Thank you very much!!  (and for making this default in 5.4)
  3. Markus Jul 11, 2014
    Problem seams to still exist in 7.0 :-(
  4. Markus Jul 16, 2014

    It's a bug http://feedback.telerik.com/Project/153/Feedback/Details/133686-link-parser-does-not-work-in-dynamic-item-widget-in-list-mode

    Markus

  5. Nathan Apr 22, 2015
    In sitefinity 7, this is still a bug if you choose "No limit and paging - Show all published items at once" in the list view settings. URLs are not parsed. Work-around is to set use paging, or use limit with a high count.
  6. Ben Mar 31, 2017
    Thanks Nathan, still a bug in 8.1.
  7. Lindy Jun 09, 2018
    To get approval for higher education loans borrower have to meet certain eligibility criteria.. Lenders of this loan offer quick money to borrowers who are applying for these loans.. This kind of security guarantees the loan has to be repaid and incase the borrower defaults in payments, the property is seized from the lender.. arrow head advance short term loans direct lender
  8. Beverly Jun 10, 2018
    Generally, the interest rate on these loans are very reasonable, and therefore the amount of reimbursement would also not be terribly high.. Once the form is submitted the rest of the loan process would not take more than 24 hours to complete.. Although most of us balk on the thought of going for business loans we are left without a different choice.. northern plains funding guaranteed approval loans for bad credit

    Leave a comment