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

Forums / Developing with Sitefinity / Get ImageUrl of single image in image library

Get ImageUrl of single image in image library

8 posts, 0 answered
  1. Mike
    Mike avatar
    221 posts
    Registered:
    19 Aug 2008
    24 Feb 2009
    Link to this post
    Ok, this might sound like a simple/dumb question but i can't seem to find any reference to what i am trying to do...

    Basically what i need to do is get the URL on an image in the library...

    I know how to get the library and list of images in the library:

    ILibrary theLibrary = this.Manager.GetLibrary("Destination Deals (Thumbnails)"); 
    IList listOfImages = this.Manager.GetContent(0, 0, "Publication_Date DESC", theLibrary.ID); 

    I even know how to loop through this list and get the Url of each image in the Library:

    foreach (IContent CurrentLibraryItem in listOfImages) 
        string ImageUrl = this.ResolveUrl(String.Concat(CurrentLibraryItem.Url, this.Manager.Provider.ContentExtension)); 

    But i do not want to have to loop through the whole library just to get the url of the image i need... I nfact i even know what the image is called in the library i just need to check if it exists there and if it does return the Url for the image... without having to loop through the whole library...

    If any of that makes sense to anyone, please could you post me some code to do this...

    Duncan
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    24 Feb 2009
    Link to this post
    Hi Duncan,

    I tried different ways, but the only way that I could come up is using a filter for the library and then using ContentUrlRewriterService.FormatURL to return the url.

    Here is my code.

           string itemName = "MyImage"
            LibraryManager libMan = new LibraryManager("Libraries"); 
            ILibrary lib = libMan.GetLibrary("Images"); 
            List<IMetaSearchInfo> filter = new List<IMetaSearchInfo>(); 
            filter.Add(new MetaSearchInfo(MetaValueTypes.ShortText, "Name", itemName, SearchCondition.Equal, JoinType.And)); 
            IList cntList = libMan.GetContent(filter.ToArray());  
             
            // do this only for the first occurance 
            if (cntList.Count > 0) 
            { 
                IContent cnt = cntList[0] as IContent; 
                string url = ContentUrlRewriterService.FormatURL(cnt, libMan.Provider, ContentUrlRewriterService.FormatOptions.Path); 
                Response.Write(url); 
            } 

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Mike
    Mike avatar
    221 posts
    Registered:
    19 Aug 2008
    24 Feb 2009
    Link to this post
    Hi Ivan,

    The only reason i ask is because there seems as thou there should be a methods of the LibraryManager called GetItem() maybe with some overloads to wehre you can get a item out of the library based on the name or GUID or whatever.

    In any case i have resorted to looping through the IList sent back by the GetContent() method...

    ILibrary theLibrary = this.Manager.GetLibrary("Thumbnails"); 
    IList listOfImages = this.Manager.GetContent(0, 0, "Publication_Date DESC", theLibrary.ID); 
     
    foreach (XmlNode location in LocationNodes) 
        string ID = location.SelectSingleNode("ID").InnerText; 
     
        foreach (IContent CurrentLibraryItem in listOfImages) 
        { 
            if (CurrentLibraryItem.GetMetaData("Name").ToString() == ID) 
            { 
                ImageUrl = this.ResolveUrl(String.Concat(CurrentLibraryItem.Url, this.Manager.Provider.ContentExtension)); 
                break
            } 
        } 

    This should work for now... Thank you again for the reply...

    Duncan
  4. Slavo
    Slavo avatar
    295 posts
    Registered:
    24 Sep 2012
    25 Feb 2009
    Link to this post
    Hi Duncan,

    Since you know the ID of the item that you need the URL for, you can use the GetContent() method and pass a Guid to it. This overload will only return one IContent object and you don't have to loop through the whole library. Is this what you want?

    Guid itemID = new Guid(location.SelectSingleNode("ID").InnerText); 
    IContent currentItem = this.Manager.GetContent(itemID); 


    Best wishes,
    Slavo
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. Mike
    Mike avatar
    221 posts
    Registered:
    19 Aug 2008
    25 Feb 2009
    Link to this post
    Slavo,

    Thanks for the reply. Thi sis actually exactly what i need with one thing... I only have the name of the image not the GUID... So i would have to find the guid first somehow...

    Is there a way to get a item or item GUID by its name?

    Duncan
  6. Slavo
    Slavo avatar
    295 posts
    Registered:
    24 Sep 2012
    26 Feb 2009
    Link to this post
    Hello Duncan,

    If you only know the name of the image, you would have to revert to filtering, as the API doesn't provide a direct method. Names and Titles are usually stored as default metadata fields, and metadata is not guaranteed to be uniqe in its implementation. You can only ensure it is uniqe for specific scenarios.
    This is the reason that there's no method to get an item by name out of the box.
    You can use one of the overloads of GetContent that accepts a filter parameter and supply a MetaSearchInfo object filtering the name.

    Kind regards,
    Slavo
  7. Deven
    Deven avatar
    3 posts
    Registered:
    03 Apr 2011
    18 May 2011
    Link to this post
    Hi, I have to access an image library which I have created in sitefinity and now i want to give the user the ability to select images on the basisi of roles? any suggestions
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    18 May 2011
    Link to this post
    Hello Mike,

    There are two blog posts that you can check

    • http://www.sitefinity.com/blogs/teamblog/posts/09-05-06/hiding_libraries_based_on_user_role.aspx
    • http://www.sitefinity.com/blogs/teamblog/posts/09-05-11/hiding_libraries_based_on_user_role_-_part_ii.aspx

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
8 posts, 0 answered