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

Forums / Developing with Sitefinity / External Link associated with image in an imagelibrary

External Link associated with image in an imagelibrary

6 posts, 0 answered
  1. Scott
    Scott avatar
    11 posts
    Registered:
    17 Oct 2008
    21 Oct 2009
    Link to this post
    I am building a custom image library for a radrotator on our home page. The client wants to be able to add a url to the image so when the image is clicked on the user will be taken to that url.

    Here is what I have so far.

    I have added to the web.config a new metafield "LinkUrl"

     

    <add key="Libraries.LinkUrl" valueType="ShortText" visible="True" searchable="True" sortable="True" defaultValue="" /> 
     
     
    and
    <add name="Rotator" title="Rotator Library" defaultExtenstions=".png, .jpg, .jpeg, .gif" metaKeys="Author, AlternateText, Extension, Name, Height, Width, Size, Category, LinkUrl">  
            </add> 

    I have my Admin module set up and is working perfectly the LinkUrl gets stored and is retrieved.

    My problem lies in the rotator code I am using in my usercontrol, I can get all information except the new metadata. On line 19 I want to add the metadata LinkUrl to the rotatorData.Rows.Add statement.
    How do I gain access to the metadata for this image.
    1  //Create data table  
    2             DataTable rotatorData = new DataTable();  
    3             rotatorData.Columns.Add("Image");  
    4             rotatorData.Columns.Add("Width");  
    5             rotatorData.Columns.Add("Height");  
    6             rotatorData.Columns.Add("LinkUrl");  
    7  
    8             //get Images from gallery  
    9             IList listOfImages = getImages(Gallery);  
    10              
    11  
    12             //check if there are any images  
    13             if (listOfImages.Count > 0)  
    14             {  
    15                 //loop through each image and store url into datatable  
    16                 foreach (IContent tempInfo in listOfImages)  
    17                 {  
    18                     //remove the ~ from the url, resize the images to match the dimensions of the rotator control  
    19                     rotatorData.Rows.Add(new string[] { tempInfo.UrlWithExtension.Replace("~"""), rotator.Width.ToString(), rotator.Height.ToString(), tempInfo.GetMetaData("LinkUrl").ToString() });  
    20                  
    21                 }  
    22                 rotator.DataSource = rotatorData;  
    23                 rotator.DataBind();  
    24             }  
    25             //if there are no images, hide the title and hide the rotator  
    26             else 
    27             {  
    28                   
    29                 rotator.Visible = false;  
    30             }  
    31         }  
    32  
    33         private IList getImages(string libName)  
    34         {  
    35             //create sitefinity library manager  
    36             LibraryManager libManager = new LibraryManager("Libraries");  
    37  
    38             //try to access images, if the gallery does not exist the GetLibrary method throws an unhandled exception. The method will throw an error stating the library doesn't exist.  
    39             try 
    40             {  
    41                //get library object  
    42                 ILibrary lib = libManager.GetLibrary(libName);  
    43  
    44                 //get Library ID  
    45                 Guid[] parentIDs = new Guid[1];  
    46                 parentIDs[0] = lib.ID;  
    47  
    48                 //get all images for specific library  
    49                 IList listOfImages = libManager.GetContent(0, 0, "Publication_Date DESC", parentIDs);  
    50                 
    51                 return listOfImages;  
    52             }  
    53             catch 
    54             {  
    55                 throw new Exception("Error: Gallery does not exist");  
    56             }  
    57  
    58               
    59  
    60         } 

    Thanks in advance
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    23 Oct 2009
    Link to this post
    Hi Scott,

    I created a simple RadRotator control with the following code behind  and the DataTable was correctly populated.

    protected void Page_Load(object sender, EventArgs e)
       {
           var rotatorData = new DataTable();
           rotatorData.Columns.Add("Image");
           rotatorData.Columns.Add("Width");
           rotatorData.Columns.Add("Height");
           rotatorData.Columns.Add("LinkUrl");
           string Gallery = "Images";
     
           IList listOfImages = getImages(Gallery);
           var libManager1 = new LibraryManager("Libraries");
           if (listOfImages.Count > 0)
           {
               foreach (IContent content in listOfImages)
               {
                   string MetaData = (string)content.GetMetaData("LinkUrl");
                   IContent stg = (IContent)libManager1.GetContent(content.ID);
                   if (string.IsNullOrEmpty(MetaData))
                   {
                                                                    // THE URL WAS NOT ENTERED, DO SOMETHING HERE
                            
                   }
     
                   DataRow row = rotatorData.NewRow();
                   row["Image"] = stg.UrlWithExtension.Replace("~", "").ToString();
                   row["Width"] = rotator.Width.ToString();
                   row["Height"] = rotator.Height.ToString();
                   row["LinkUrl"] = stg.GetMetaData("LinkUrl").ToString();
                   rotatorData.Rows.Add(row);
     
     
               }
               rotator.DataSource = rotatorData;
               rotator.DataBind();
           }
           else
           {
     
               rotator.Visible = false;
           
     
     
       }
     
       private IList getImages(string libName)
       {
     
           var libManager = new LibraryManager("Libraries");
           ILibrary lib = libManager.GetLibrary(libName);
           Guid[] parentIDs = new Guid[1];
           parentIDs[0] = lib.ID;
           IList listOfImages = libManager.GetContent(0, 0, "Publication_Date DESC", parentIDs);
           return listOfImages;
       }

    Please give a try to the sample above. Also make sure that you have your metakey added under libraryInfo

    <libraryInfo>  
    <add name="Image" title="Image Library" defaultExtenstions=".png, .jpg, .jpeg, .gif" metaKeys="LinkUrl, Author, AlternateText, Extension, Name, Height, Width, Size, Category"></add
     
     
    ....
        ...
    </libraryInfo>


    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Bart Welch
    Bart Welch avatar
    64 posts
    Registered:
    10 Jul 2009
    27 Nov 2009
    Link to this post
    Ivan,

    By adding the LinkUrl to the metakey in web.config, will that give us the ability to add a link to an image whenever that image is used on the web site?
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    27 Nov 2009
    Link to this post
    Hello Bart Welch,

    metakeys gives you the ability to persists some data without creating a new data layer. You can create unlimited number of metakeys for each Generic Content based module.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Graham
    Graham avatar
    6 posts
    Registered:
    18 Sep 2006
    16 Feb 2010
    Link to this post

    I have added a new metaKey column in my web.config; how do I get the new column to show up in my edit page for the image?

    My new definition is:

     

    <

     

    add name="ProductImage" title="Product Image Library" defaultExtenstions=".png, .jpg, .jpeg, .gif" metaKeys="Author, Extension, Name, Height, Width, Size, Category, SKU">

     


    How do I get a SKU textbox to show up on my image edit page in SiteFinity admin.
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    19 Feb 2010
    Link to this post
    Hi Graham,

    As far as I see it you have not added a metakey. You have added a new library type.

    The metakey should be added inside web.config's <metaFields> section

    <add key="Libraries.TEST" valueType="ShortText" visible="True" searchable="True" sortable="True" defaultValue="" />

    then the key should be registered in libraryInfo section - metaKeys

    You have to map LibraryItemEdit template ( how to map a template). Then add an ITextControl with ID="ProductImage" inside ContentMetaFields control declaration

    <sfGCn:ContentMetaFields ID="imageMetaFields" runat="server">
     
     
    .....
     
     
    </sfGCn:ContentMetaFields>


    Best wishes,
    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.
Register for webinar
6 posts, 0 answered