Thursday, March 19, 2015

Access and Resolve RTF component field in C# TBB


Tridion Bite : Use TemplateUtilities. ResolveRichTextFieldXhtml(string inputXhtml) method to resolve value of a RTF field.

Recently came across the issue, when we access RTF field value in c# TBB  and add it to the output; the link resolver wasn’t resolving the component links inside the rtf field and having some unnecessary namespaces like “xmlns="http://www.w3.org/1999/xhtml” in the output.

The value we get in C# TBB is an anchor string like below (as it is stored in component as XHTML)
<a xmlns:xlink="http://www.w3.org/1999/xlink" class="plain-link" xlink:href="tcm:x-y" title="someTitle" xlink:title="someTitle">linkText</a>

But link resolver tbb expects it in like below to resolve it
<a class="plain-link" title="someTitle" tridion:href="tcm:x-y" xmlns:tridion="http://www.tridion.com/ContentManager/5.0">linkText</a>


With a little investigation we found that the static Method TemplateUtilities. ResolveRichTextFieldXhtml(string inputXhtml) comes handy for it. it accepts the xhtml from the RTF field and converts the xhtml anchor links to the format link resolver expects. This also removes unwanted namespaces as well.