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

Forums / Designing with Sitefinity / How do I display a SiteFinity Image on a Custom Control?

How do I display a SiteFinity Image on a Custom Control?

8 posts, 0 answered
  1. Mark
    Mark avatar
    8 posts
    Registered:
    23 Jan 2009
    11 Mar 2010
    Link to this post
    Hello,

    I have an image with sitefinity ID: [Libraries]9b614f33-9307-4d1d-b3b9-7db6eba83fe4

    What sitefinity user control do I use to display the image?

    Regards,
    Mark.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    11 Mar 2010
    Link to this post
    Hi Mark,

    You need to create a custom control to resolve the url - Resolving dynamic links

    Regards,
    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. Mark
    Mark avatar
    8 posts
    Registered:
    23 Jan 2009
    11 Mar 2010
    Link to this post
    Hello Ivan,

    Whan you upload an image in SiteFinity you can add 'alt text' for the image.

    Is there any SiteFinity Control or that gets the 'image' and the 'alt text'?

    Regards,
    Mark.
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    11 Mar 2010
    Link to this post
    Hi Mark,

    You can take a look at the Image control in section Most Popular. It takes an image buy [ProviderName]Guid, resolves the Url and lets you set alt text when you insert an image. The control you can create would basically get the image and after resolving the url will assign this to <asp:Image> control, you can also make the control set AlternateText property of the <asp:Image> control.

    Greetings,
    Radoslav Georgiev
    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. Mark
    Mark avatar
    8 posts
    Registered:
    23 Jan 2009
    11 Mar 2010
    Link to this post
    The Image is already in the Database.
    The alt text is already in the Database.

    How do I get the alt text from the database?

    Mark.
  6. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    11 Mar 2010
    Link to this post
    Hi Mark,

    The alternate text is stored as a metafield. Use LibraryManager to get the content item and then get the alt text metafield:
    LibraryManager libraryManager = new LibraryManager();
    IContent cntItem = libraryManager.GetContent(ImageID);
    string alternateText = cntItem.GetMetaData("AlternateText") as string;


    All the best,
    Radoslav Georgiev
    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. Mark
    Mark avatar
    8 posts
    Registered:
    23 Jan 2009
    12 Mar 2010
    Link to this post
    Hello,

    Thanks for the Help.

    If you need to display a SiteFinity image in a custom control I found that you can use the Telerik.Cms.Engine.WebControls.SitefinityImage control, but it will not add the Image Alt Text automatically.

    I have added functionality that will add the alt text to the image automatically.

    If you are passing the ImageID to the ImageUrl property you can use my upgraded SitefinityImage to display the image and add the Alt Text automatically. An ImageID looks like this: [Libraries]9b614f33-9307-4d1d-b3b9-7db6eba83fe4

    If you are passing a URL to the ImageUrl property then the Alt Text will not be automatically added, although somebody may be able to add this functionality.

    Below is the code for the upgraded server control:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text.RegularExpressions;
     
    namespace TelerikServerControls
    {
        /// <summary>
        /// <para>Adds the Alternate Text That was assigned to the image when uploaed to SiteFinity.</para>
        /// <para>Use Image ID as the ImageUrl. E.g. [Libraries]9b614f33-9307-4d1d-b3b9-7db6eba83fe4</para>
        /// </summary>
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:SiteFinityImage runat=server></{0}:SiteFinityImage>")]
        public class SiteFinityImage : Telerik.Cms.Engine.WebControls.SitefinityImage
        {
            protected override void OnLoad(EventArgs e)
            
                this.PreRender += new EventHandler(SiteFinityImage_PreRender);          
            }
     
            protected void SiteFinityImage_PreRender(object sender, EventArgs e)
            {
                //Use pre render event to get Alt Text. If ImageUrl is bound to a dataItem in a RadGrid then the code below must be called in Pre Render. If you try to do in PageLoad the this.ImageUrl is not populated yet.           
                //debug
                //HttpContext.Current.Response.Write(this.ImageUrl);
                
                #region Assign Alternate Text to the image
     
                //~~~~ Try to get GUID from Image url string ~~~~
                Nullable<Guid> ImageGuid = GetGuid(this.ImageUrl);
     
                //~~~~ if GUID found, use it to get the Alt text. ~~~~
                if (ImageGuid.HasValue)
                {
                    this.AlternateText = GetAltText(ImageGuid.Value);
                }         
     
                #endregion
                 
            }
     
            /// <summary>
            /// <para>Method to get alt text automatically.</para>
            /// <para>Takes a Guid</para>
            /// <para>Returns a string</para>
            /// </summary>
            /// <param name="ImageID"></param>
            /// <returns></returns>
            public string GetAltText(Guid ImageID)
            {
                //Use LibraryManager to Get the Image as a CMS Content object
                Telerik.Libraries.LibraryManager libraryManager = new Telerik.Libraries.LibraryManager();
                Telerik.Cms.Engine.IContent cntItem = libraryManager.GetContent(ImageID);
                //Get the MetaData item called 'AlternateText' for the CMS Content Object
                return cntItem.GetMetaData("AlternateText") as string;      //The Alt Text is Stored as a MetaData Object
            }       
     
            /// <summary>
            /// <para>Returns the First Guid found in a String</para>
            /// <para>Returns Nullable<Guid> if NO GUID found</para>
            /// </summary>
            /// <param name="StringContainingGuid"></param>
            /// <returns></returns>
            public static Nullable<Guid> GetGuid(string StringContainingGuid)
            {
                //Reg exp to match a GUID
                Regex re = new Regex(@"(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.IgnoreCase);
     
                //find the GUID
                Match GuidMatch = re.Match(StringContainingGuid);
     
                if (GuidMatch.Success)
                {
                    return new Guid(GuidMatch.Value);
                }
                else
                {
                    return null;
                }
            }
     
            //Renders the Image HTML
            protected override void RenderContents(HtmlTextWriter output)
            {
                //~~~~ Call base render method ~~~~
                base.RenderContents(output);
            }
             
        }
     
    }


    Comments welcome.

    Regards,
    Mark.
  8. James
    James avatar
    25 posts
    Registered:
    15 May 2009
    24 Jun 2010
    Link to this post
    Hi Mark,

    Have you seen the Sample Products Module? The original Sitefinity 3.6 release post by Ivan says it would be included in the backend last February, however it has not been. People like myself are still having problems with its implementation.. specifically I am having a problem (link) with displaying the resolved Url back to the control. It seems you have a fair bit of knowledge regarding translating the GUID to a url. Once I can resolve the ImageID I need to look at Alt Text for the same control. I was wondering if you might have any suggestions as to how I get my code to work? Thanks in advance if you have the time.

    JJ
Register for webinar
8 posts, 0 answered