{"id":403,"date":"2015-12-21T14:05:19","date_gmt":"2015-12-21T13:05:19","guid":{"rendered":"http:\/\/jonika.nu\/JonasBlogg\/?p=403"},"modified":"2015-12-21T18:09:25","modified_gmt":"2015-12-21T17:09:25","slug":"resolving-links-in-xhtmlstrings","status":"publish","type":"post","link":"https:\/\/jonika.nu\/JonasBlogg\/archives\/403","title":{"rendered":"Resolving links in XhtmlStrings, EPiServer 7.16.1"},"content":{"rendered":"<p>Did you ever try to store a XhtmlString in a variable and use it in your code? Maybe you noticed that some stuff, like links, isn&#8217;t very useful because they are internal.<\/p>\n<p>I noticed this when i upgraded a site from CMS6, and on that site i had a working code that rendered dynamic content so the code i passed to my variable actually was useful, but it broke when i upgraded the site. What i did find than, was that XhtmlStrings are made up of Fragments. And that&#8217;s really useful!<\/p>\n<p>One thing you can do with these fragments is resolving internal links, and this is because links are stored as a UrlFragment. A typical XhtmlString could look like:<\/p>\n<p><a href=\"http:\/\/jonika.nu\/JonasBlogg\/wp-content\/uploads\/2015\/12\/fragment_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-404\" src=\"http:\/\/jonika.nu\/JonasBlogg\/wp-content\/uploads\/2015\/12\/fragment_1.png\" alt=\"\" width=\"579\" height=\"157\" srcset=\"https:\/\/jonika.nu\/JonasBlogg\/wp-content\/uploads\/2015\/12\/fragment_1.png 579w, https:\/\/jonika.nu\/JonasBlogg\/wp-content\/uploads\/2015\/12\/fragment_1-300x81.png 300w\" sizes=\"(max-width: 579px) 100vw, 579px\" \/><\/a>As you can see, we have regular content, UrlFragments and a ContentFragment. So now, we can write some code to do stuff with different fragements:<\/p>\n<pre class=\"lang:c# decode:true\">StringBuilder sBuilder = new StringBuilder();\r\nforeach (IStringFragment sFragment in Xhtml.Fragments)\r\n{\r\n  if (sFragment is ContentFragment)\r\n  {\r\n  }\r\n  if (sFragment is UrlFragment)\r\n  {\r\n  }\r\n  sBuilder.Append(sFragment.GetViewFormat());\r\n}<\/pre>\n<p>Maybe you would like to do something with a block?<\/p>\n<pre class=\"lang:c# decode:true\">if (sFragment is ContentFragment)\r\n{\r\n  ContentFragment cFragment = sFragment as ContentFragment;\r\n  var Content = Repository.Get&lt;ContentData&gt;(cFragment.ContentLink);\r\n}\r\n<\/pre>\n<p>Or, if you would like to resolve links in your XhtmlString:<\/p>\n<pre class=\"lang:c# decode:true \">if(sFragment is UrlFragment) \r\n{\r\n  UrlFragment uFragment = sFragment as UrlFragment;\r\n  UrlBuilder uBuilder = new UrlBuilder(uFragment.InternalFormat);\r\n  Global.UrlRewriteProvider.ConvertToExternal(uBuilder, null, Encoding.UTF8);\r\n  sBuilder.Append(uBuilder.Uri);\r\n  continue;\r\n}\r\n<\/pre>\n<p>The complete code i use to resolve links before passing the content to a javascript:<\/p>\n<pre class=\"lang:c# decode:true \">public static string ParseXhtmlString(XhtmlString Xhtml)\r\n{\r\n    StringBuilder sBuilder = new StringBuilder();\r\n    if (!(Xhtml == null || Xhtml.IsEmpty))\r\n    {\r\n        foreach (IStringFragment sFragment in Xhtml.Fragments)\r\n        {\r\n            UrlFragment uFragment = sFragment as UrlFragment;\r\n            if(uFragment != null) \r\n            {\r\n                try\r\n                {\r\n                    UrlBuilder uBuilder = new UrlBuilder(uFragment.InternalFormat);\r\n                    Global.UrlRewriteProvider.ConvertToExternal(uBuilder, null, Encoding.UTF8);\r\n                    sBuilder.Append(uBuilder.Uri);\r\n                    continue;\r\n                }\r\n                catch (Exception)\r\n                { }\r\n            }\r\n            sBuilder.Append(sFragment.GetViewFormat());\r\n        }\r\n    }\r\n    return sBuilder.ToString();\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<a href=\"https:\/\/twitter.com\/jonlin76\" class=\"twitter-follow-button\" data-show-count=\"false\" data-size=\"small\">Follow jonlin76<\/a>\n<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=\/^http:\/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+':\/\/platform.twitter.com\/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');<\/script>","protected":false},"excerpt":{"rendered":"<p>Did you ever try to store a XhtmlString in a variable and use it in your code? Maybe you noticed that some stuff, like links, isn&#8217;t very useful because they are internal. I noticed this when i upgraded a site from CMS6, and on that site i had a working code that rendered dynamic content &hellip; <a href=\"https:\/\/jonika.nu\/JonasBlogg\/archives\/403\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Resolving links in XhtmlStrings, EPiServer 7.16.1<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[92,90,93,91],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts\/403"}],"collection":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/comments?post=403"}],"version-history":[{"count":4,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts\/403\/revisions"}],"predecessor-version":[{"id":408,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts\/403\/revisions\/408"}],"wp:attachment":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/media?parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/categories?post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/tags?post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}