More in this section

Forums / Developing with Sitefinity / Accessing Image URL from templates

Accessing Image URL from templates

2 posts, 0 answered
  1. Anthony
    Anthony avatar
    9 posts
    15 Nov 2011
    07 Feb 2012
    Link to this post


    We are currently using sitefinity 4.4 and looking to take advantage of the feature which allows developers to append “size=max width or height”  to the end of a query string. Then the server would resize the image based up on the size parameter.



    Resizes to a  maximum width or height of 640.

    Our current problem is we want to add the size parameter to the query string within widget templates so we need access to the url of the following fields.

    <img src="<%# Eval("PrimaryImageUrl") %>" />

      <sf:AssetsField runat="server" DataFieldName="HeroImage" />

    We were hoping for something like

     <%# ( (ContentLink[]) Eval("ThumbnailImage") )[0].URL %>


    Is this or anything similar possible?

  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    18 Jul 2012
    08 Feb 2012
    Link to this post
    Anthony, this should certainly be possible. The approach I use is to place a helper method in the code-behind of the template. This requires mapping an external widget template, but gives you more control over the rendering of the content properties.

    What I usually do is do define a method like this:

    protected string RenderImage(object contentItem)
        var item = contentItem as MyContentItem;

    This allows me to pass the entire content item from the template:
    <img src="<%# RenderImage(Container.DataItem) %>" />

    In the method above, MyContentItem can be your custom object, or one of the built-in content items like News, or BlogPost, etc.

    Adding a breakpoint in that method allows you to inspect all the properties to extract the value you might need, construct a string response, and return it to the template.

    For an example of using a helper method to retrieve an image, take a look at this article: Retrieving Data From Dynamic Modules Using the Module Builder API

    Hope this is helpful!
2 posts, 0 answered