More in this section

Forums / Developing with Sitefinity / Header Image Based on News Item

Header Image Based on News Item

2 posts, 0 answered
  1. Tony
    Tony avatar
    23 posts
    Registered:
    12 Sep 2008
    10 Dec 2008
    Link to this post
    I am trying to write a WebControl based on the current news item.

    I have two pages:

    Newsletters (lists all the latest news)
      - NewsItem (displays a single news item)

    On NewsItem I want the header image to be based on the actual news article so it would therefore be different per news article.

    I am fine doing this either programatically calling the database based on some parameter or by using whatever API sitefinity has for getting a news article's properties.

    I don't know how to get the parameter to call the db or how to use the API for this.

    I tried copying the single news item template to my control to get the title (as a test) but it doesn't work.

    My best solution would be to pull the thumbnail image that is already in the properties and just remove part of it for the full image:

    ex. myimage_thumb.jpg -> myimage.jpg

    Anyone know how I can do this?
  2. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    11 Dec 2008
    Link to this post
    Hello Tony,

    I have prepared a short sample demonstrating how to get the content item being currently displayed based on the url. You will need to modify it to get your actual scenario working. Here is the sample:

    using System;  
    using System.Data;  
    using System.Configuration;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.HtmlControls;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    using Telerik.Cms.Engine;  
    using Telerik.Web;  
     
    /// <summary>  
    /// Summary description for HeaderControl  
    /// </summary>  
    public class HeaderControl : CompositeControl  
    {  
        /// <summary>  
        /// Gets or sets the name of the provider  
        /// </summary>  
        public string ProviderName  
        {  
            get 
            {  
                object obj = ViewState["ProviderName"];  
                if(obj != null)  
                    return (string) obj;  
                return "News";  
            }  
            set 
            {  
                ViewState["ProviderName"] = value;  
            }  
        }  
     
        /// <summary>  
        /// Gets or sets the key of the content item  
        /// </summary>  
        public string ContentItemKey  
        {  
            get 
            {  
                object obj = ViewState["ContentItemKey"];  
                if(obj != null)  
                    return (string) obj;  
                return "NewsContentItem";  
            }  
            set 
            {  
                ViewState["ContentItemKey"] = value;  
            }  
        }  
     
        protected ContentManager Manager  
        {  
            get 
            {  
                if(manager == null)  
                    manager = new ContentManager(ProviderName);  
                return manager;  
            }  
        }  
     
        protected override void CreateChildControls()  
        {  
            // try to get the id of the item from the url - all content items (such as news, blogs,  
            // events pass this)  
            Guid itemId = Guid.Empty;  
            if (Context.Items.Contains(Manager.Provider.ContentItemKey))  
                itemId = (Guid)((IUrlRewriteData)Context.Items[Manager.Provider.ContentItemKey]).Data;  
            else if (!String.IsNullOrEmpty(Context.Request[ContentItemKey]))  
                itemId = new Guid(Context.Request[ContentItemKey]);  
     
            // if you have managed to extract the item id get the content item  
            if(itemId != Guid.Empty)  
            {  
                IContent content = Manager.GetContent(itemId);  
     
                // now that you have content, you can retrieve values of the meta fields and   
                // display the header image or anything else your scenario may need  
     
                // NOTE: by default HeaderImageUrl meta key is not defined, so this is up to you   
                // how will you get the url of the header image. This sample demonstrates how to  
                // recongize which content item is being displayed on the current page  
                string headerImageUrl = string.Empty;  
                if(Manager.MetaKeys.Contains("HeaderImageUrl"))  
                    headerImageUrl = content.GetMetaData("HeaderImageUrl").ToString();  
     
                // do something with headerImageUrl  
            }  
     
        }  
     
        ContentManager manager;  

    Bear in mind that ContentItemKey and ProviderName properties must match the properties of the item being displayed. So for example if you have a NewsView control that displays single news item, make sure that those two properties are identical on NewsView and your Header control.

    Also, you will probably need to add a meta field that will hold the url of the header image.

    I hope you will find this information helpful. Let us know if there is anything else we can do for you.

    Greetings,
    Ivan
    the Telerik team

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