+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Rendering Images From Libraries in List Item Programmatically

Rendering Images From Libraries in List Item Programmatically

5 posts, 0 answered
  1. chrishawn
    chrishawn avatar
    5 posts
    Registered:
    23 Jan 2009
    24 Mar 2009
    Link to this post
    Background:

    I am making a usercontrol binding content from ListItems
    using ListItem.Content but when I put an Image in that list item
    it comes back in the string as
     
    <img src=\"[Libraries]6f93dc67-662c-40a9-b8f3-181620e01e99\"> 

    Is there a method I can call on the Content String to turn that src into the correct path to the image without having to parse it?

    --
    I am using version 3.6
  2. Parvan
    Parvan avatar
    151 posts
    Registered:
    24 Sep 2012
    24 Mar 2009
    Link to this post
    Hi chrishawn,

    You could use this code to resolve the url:
    public string GetItemUrl(string unresolvedUrl) 
        string urlResolved = null
        if (unresolvedUrl.StartsWith("[")) 
        { 
            int idx = unresolvedUrl.IndexOf("]"); 
            string provider = unresolvedUrl.Substring(1, idx - 1); 
            string strId = unresolvedUrl.Substring(idx + 1); 
            Guid id = new Guid(strId); 
     
            if (Telerik.Cms.Engine.ContentManager.Providers.ContainsKey(provider)) 
            { 
                Telerik.Cms.Engine.IContent cnt = Telerik.Cms.Engine.ContentManager.Providers[provider].GetContent(id); 
                if (cnt != null
                    urlResolved = cnt.UrlWithExtension; 
            } 
        } 
        if (string.IsNullOrEmpty(urlResolved)) 
        { 
            return String.Concat("Item not found: " + unresolvedUrl); 
        } 
        else 
        { 
            if (urlResolved.StartsWith("~/")) 
            { 
                urlResolved = VirtualPathUtility.ToAbsolute(urlResolved); 
            } 
            return urlResolved; 
        } 

    Pass to it the unresolved url and it will return the url to the actual image:
    string resolvedUrl = 
      GetItemUrl("[Libraries]6f93dc67-662c-40a9-b8f3-181620e01e99"); 

    I hope this will help you.

    Regards,
    Parvan
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  3. chrishawn
    chrishawn avatar
    5 posts
    Registered:
    23 Jan 2009
    24 Mar 2009
    Link to this post
    don't you think it would be better for your API in the future to provide automatic parsing for things like this

    so i could get a generic content and
    do something like:
        string LiteralText = ListItem.Content.ResolvedText();
    or
        string LiteralText = ContentManager.ResolveText(ListItem.Content);

    then i don't have to dance around sitefinity's internal mechanisms and when/if you change them in the future my code doesn't have to break/change
  4. chrishawn
    chrishawn avatar
    5 posts
    Registered:
    23 Jan 2009
    24 Mar 2009
    Link to this post
    The other problem with the given approach is since this is generic content is it is now up to me to have to parse first for any weird guid-bracket combinations.
  5. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    25 Mar 2009
    Link to this post
    Hi,

    I think that we should provide new methods for the resolving of these items, rather than changing the existing. Changing the existing could break the backward compatibility.
    I will bring this question to the team.

    Thank you for the additional notes!

    Greetings,
    Georgi
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Register for webinar
5 posts, 0 answered