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

Forums / Developing with Sitefinity / Getting thumbnail URL for an item in ImageLibrary

Getting thumbnail URL for an item in ImageLibrary

8 posts, 0 answered
  1. H N
    H N avatar
    4 posts
    Registered:
    18 Sep 2009
    11 Nov 2010
    Link to this post
    Hi,

    I am new to Sitefinity so this may sound a silly question. I have a simple NewsList control that will display the thumbnail and the title of the news item. Here is what i do in the repeater control :

    IContent i = (IContent)e.Item.DataItem;
    string title = i.GetMetaData("Title").ToString();
    string thumbnail = i.GetMetaData("Thumbnail").ToString();        
    ((Label)e.Item.FindControl("lblTitle")).Text = title;
    ((Image)e.Item.FindControl("imgThumbnail")).ImageUrl = thumbnail;

    I am getting the thumbnail value as "[Libraries]55c14ef5-036e-4f26-8f47-e98959c99229" rather than a URL that i can assign to the imageUrl property. Is there a quick way to get this ?

    Any help will be appreciated.

    Thanks.
    H
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    11 Nov 2010
    Link to this post
    Hello H N,

    You have to resolve the dynamic link - Resolving the url of dynamic links

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. H N
    H N avatar
    4 posts
    Registered:
    18 Sep 2009
    21 Nov 2010
    Link to this post
    I used this and it was returning me the image URL as i expected. But suddenly i find that it is now returning "null" for the following line :
    IContent cnt = ContentManager.Providers[provider].GetContent(id);

    Am i doing anything wrong in this function which is suppose to give me the URL of the item :

    public string GetItemUrl(string thumb, bool resolveAsAbsoluteUrl)
        {
            string re1 = ".*?"; // Non-greedy match on filler
            string re2 = "(Libraries)"; // Variable Name 1
            string provider = "", id = "";
            Regex r = new Regex(re1 + re2, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            Match m = r.Match(thumb);
            if (m.Success)
            {
                provider = m.Groups[1].ToString();
                id = thumb.Replace("[" + provider + "]", "");
                Guid guid = new Guid(id);   
            if (ContentManager.Providers.ContainsKey(provider))
            {
                IContent cnt = ContentManager.Providers[provider].GetContent(id);
                if (cnt != null)
                    return VirtualPathUtility.ToAbsolute(cnt.UrlWithExtension, this.Context.Request.ApplicationPath);
            }
            else
            {
                // we assume it is a page  
                SiteMapNode node = SiteMap.Provider.FindSiteMapNodeFromKey(id.ToString());
                if (node != null)
                    return this.ResolveClientUrl(node.Url);
            
              
            }
            return thumb;
        }
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    21 Nov 2010
    Link to this post
    Hello H N,

    You are using a wrong provider or there is not item with the ID you are passing to GetContent method.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. H N
    H N avatar
    4 posts
    Registered:
    18 Sep 2009
    21 Nov 2010
    Link to this post

    There is only one Image Library that i am using as provider. When i debug it i see the "Libraries"  as a provider coming back from the following line of code:

    string thumbnail = GetItemUrl(i.GetMetaData("Thumbnail").ToString(), true);

    Is there is something i am missing here ? Please see attached file which will show the screen shot of debug window with the locals information.

  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    21 Nov 2010
    Link to this post
    Hello H N,

    Then there is no item with this ID found in the provider. Could you try to use an instance of ContentManager and then call GetContent. Pass the provider as a string inside the constructor of ContentManager.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. H N
    H N avatar
    4 posts
    Registered:
    18 Sep 2009
    21 Nov 2010
    Link to this post
    That's it ! It fixed the issue. Thanks for all your help. I am new to the SF development so your prompt support is much appreciated.

    Thanks.
    HN
    http://www.agilesetup.com
  8. Andrew
    Andrew avatar
    1 posts
    Registered:
    01 Aug 2008
    25 Jan 2011
    Link to this post
    I had difficulties with GetInageUrl() code snippet above. Here's my tweaked code incase anyone else finds this useful.

    public string GetItemUrl(string thumb, bool resolveAsAbsoluteUrl)
    {
        string provider = "", id = "";
        Regex r = new Regex(@"\[(\w+)\](.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
        Match m = r.Match(thumb);
        if (m.Success)
        {
            provider = m.Groups[1].ToString();
            id = m.Groups[2].ToString();
     
            Guid guid = new Guid(id);
            if (ContentManager.Providers.ContainsKey(provider))
            {
                IContent cnt = ContentManager.Providers[provider].GetContent(guid);
                if (cnt != null)
                    return VirtualPathUtility.ToAbsolute(cnt.UrlWithExtension, this.Context.Request.ApplicationPath);
            }
            else
            {
                // we assume it is a page 
                SiteMapNode node = SiteMap.Provider.FindSiteMapNodeFromKey(id.ToString());
                if (node != null)
                    return this.ResolveClientUrl(node.Url);
            }
     
        }
        else
        {
            return this.ResolveClientUrl(thumb);
        }
        return thumb;
    }
Register for webinar
8 posts, 0 answered