Resolve dynamic links
Sitefinity CMS enables you to automatically update the references you make from your content item to other content - media or pages - thus avoiding the need to update links manually every time referenced content is modified. As a result, there are no outdated content references and broken links.
For example, if your content item links to an image, Sitefinity CMS automatically resolves every reference to this image even after modifications such as changes in title, address, or even replacement with another image are done.
To correctly retrieve and persist content from and to the database, Sitefinity CMS includes an sfref attribute to the HTML tag of the referenced content. The sfref attribute holds important information - the Guid of content item you are referencing to and, depending on the type of content item, the following:
|Content type ||sfref attribute |
|Images || [images|providername]image_id |
|Pages ||[rootNodeId]page_id |
When you create a content item that links to other content or pages, this content item is persisted to the database. As a result, Sitefinity CMS injects the value of the sfref attribute as a value of the src attribute of the HTML tag of the content that you saved to the database. Upon retrieval from the database, Sitefinity CMS resolves this content item using a link parser. The DynamicLinkParser class in Sitefinity CMS is a Helper class for resolving and unresolving dynamic links in HTML formatted text. Depending on where you view the item from, the sfref attribute is resolved differently:
Both the src and sfref attributes hold their respective values. The sfref in the backend is used by Sitefinity's designer dialogs. When you open an image in the backend using a designer, the sfref attribute locates the correct content item to be opened in the designer mode.
The sfref attribute is omitted, so that it does not inflate the HTML with unnecessary information.
Resolve dynamic links programmatically
If you choose to implement your own logic for displaying HTML content of content items, you must resolve all links to other content - media or pages. To do so refer to the following example:
In the code above, you get an instance of the DynamicLinksParser class and call the Apply method. As a result all dynamic links in the specified HTML text are resolve to point to their actual location. When creating the DynamicLinksParser class instance, you can pass constructor parameters to remove the sfref attribute from the HTML and resolve URL's with their absolute path.