Converting Lstring ContentItem Data from Custom Module to HTML Image
I have a custom module with a long Text field, named Logo. It contains a single Image and other HTML. I want to display the logo image on the page, so I just assign the value to a literal, ("myCompany" is of type DynamicContent).
var logo = myCompany.GetValue<
Lstring
>("Logo").Value;
litLogo.Text = logo;
The result displays this HTML on the page:
<
img
src
=
"[images|OpenAccessDataProvider]421d54cc-469b-6485-baf1-ff00000f0a6a"
title
=
"godfrey-logo"
>
Any help is appreciated.
Coty
Going to make this a two part question, the first above. Basically how do you pull a long Text field from a custom module and displaying as HTML so that all images show up that are embedded?
Second - in looking at my custom module I added a Media field named logo that holds the company logo instead. The block below works to pull this data. Is this the correct/most efficient way to pull an image from a custom module?
//Get image from custom module
LibrariesManager libraryManager = LibrariesManager.GetManager();
var logos = myPartner.GetValue<
Telerik.Sitefinity.Model.ContentLinks.ContentLink
[]>("Logo");
foreach (var logoLink in logos)
var image = libraryManager.GetImage(logoLink.ChildItemId);
//add image to page
litLogo.Text = "<
img
src
=
'" + image.Url + "'
/img>";
Thanks!
Coty
Hi Coty,
Regarding your questions:
Long Text field
I think you should use the LinkParser to get the right html from your text field. You can use it like this:
var parser = LinkParser.ResolveLinks(
"CONTENT"
);
It has some more overloads, if you need them.
Getting an image
Your code is fine. You could make it a bit smaller like this:
var contentLinks = (ContentLink[])item.GetValue(
"myPartner"
);
var imageContentLink = contentLinks.Count() > 1 ? contentLinks.OrderBy(x => x.Ordinal).First() : contentLinks.FirstOrDefault();
litLogo.Text =
string
.Format(
"<img src='0' />"
, imageContentLink.ChildItemId.GetImage().MediaUrl);
Kind regards,
Daniel
Sorry, for this last code to work, you will need an extension method.
Just put this method in a static class, named e.g. 'Extensions':
public
static
Image GetImage(
this
Guid imageId)
var manager = LibrariesManager.GetManager();
return
(imageId != Guid.Empty) ? manager.GetImage(imageId) :
null
;
Kind regards,
Daniel