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

Forums / Developing with Sitefinity / Extending Single News

Extending Single News

5 posts, 1 answered
  1. Mike Drag
    Mike Drag avatar
    10 posts
    Registered:
    29 May 2006
    11 Aug 2009
    Link to this post
    Hi

    I am using Sitefinity 3.6  and tryng to extend the single news view to display meta-fields i have defined in templates and was able to make it work on news master view.

    I have tried reffering to example here ( http://www.sitefinity.com/support/forums/sitefinity-3-x/developing-with-sitefinity/showing-thumbnail-on-singlenews.aspx) but this is the old version of sitefinity.

    Can somebody help me how to overrie the CreateSingleContent() method to change NavigateUrl on hyperlink i have defined in singleNews template?

    Thank you very much in advance
    Mike
  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    11 Aug 2009
    Link to this post
    i'm not sure if I fully understand the question, so forgive me if i'm off a little bit here...

    are you using the NewsView control? if so, you can map the singlenews template to an external usercontrol from the embedded templates (downloadable from your sitefinity account).

    if you create a code-behind for this page, you should be able to use reflection to modify the controls on that page, as opposed to having to override the newsview control and register and replace the built-in one.

    i hope that makes sense, if this is what you're trying to do, let me know and I will try to walk you through it if you still need help
  3. Mike Drag
    Mike Drag avatar
    10 posts
    Registered:
    29 May 2006
    11 Aug 2009
    Link to this post
    Hi SelArom

    I have created my own user control and my own templates (modified ones that I downloaded). My user control inherits from NewsView (public class CustomHostNewsControl : NewsView ) .
    I have successfully implemented details list to add additional meta-fields to my control. I've done this following the user manual sample extending content view control.
    I have overriden properties SingleItemTemplatePath and ItemListTemplatePath and method SetItemMetadata to make my added meta-field (which is link) work for NavigateUrl and not Text property of hyperlink.

    Now I am trying to implement same link on SingleNews template. I have added it to the template but SetItemMetaData doesn't get called on SingleView part.
    I have tried to override CreateSingleContent() method but I am stuck. I have seen example in forum post where class inherits from SingleView but no more singleview in Sitefinity 3.x and later


    Here is sample what I do for NewsList part:

     

     

     

            protected override void SetItemMetadata(System.Web.UI.Control itemContainer, Telerik.Cms.Engine.IContent contentItem)  
            {  
                // first let's call the base implementation of the SetItemMetadata, so that it fills the values of all fields it can  
                base.SetItemMetadata(itemContainer, contentItem);  
     
                // now let's apply our own custom logic for the HostLink meta field  
                // first we need to find the control with id of HostLink in our template  
                HyperLink hostLink = itemContainer.FindControl("HostLink"as HyperLink;  
                if (hostLink != null)  
                {  
                    // we are going to set its NavigateUrl property to the value of HostLink meta field  
                    hostLink.NavigateUrl = contentItem.GetMetaData("HostLink").ToString();  
                    hostLink.Text = "Visit Host";  
                }  
     
            } 


    Thanks
    Mike

  4. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    11 Aug 2009
    Link to this post
    try using reflection in the singlenews template. on the page load, do something like:

    // retrieve the current news item  
    var view = Parent.Parent as NewsView;  
    if (view == nullreturn;  
          
    var mgr = new NewsManager("News");  
    var news = mgr.Content.GetContent(view.SelectedItemId);  
    if (news == nullreturn;  
          
    lnk.NavigateUrl = news.GetMetaData("MyMetaItem"); 

    this accesses the parent, which should be a newsview (might be different if you overrode this control to make your own). then retrieve the selected item by the current id of the newsview and retrieve the meta item.

    this is not an efficient solution, since you are essentially retrieving the item twice, but it is a quick hack that avoids the need to override yet another control.

    I hope this helps get you closer to your goal! let me know how you're progressing and i'll keep doing what I can to help!
    Answered
  5. Mike Drag
    Mike Drag avatar
    10 posts
    Registered:
    29 May 2006
    11 Aug 2009
    Link to this post
    Your solution worked perfectly :-)
    Thank you very much.

    Here is what I was trying to do in my custom control that inherits from NewsView just as a reference:

    protected override void  CreateSingleContent()  
            {  
                base.CreateSingleContent();  
     
                if(this.BehaviorMode == BehaviorModes.Detail)  
                {  
     
                    IContent cnt = base.Manager.GetContent(base.SelectedItemId) as IContent;    
                      
                    foreach (IMetaInfo info in base.Manager.MetaKeys.Values)     
                    {     
                        if (info.Key == "HostLink")     
                        {  
                            HyperLink lnk = ; // I couldn't find the hyperlink control  
                            if (metaData != null)  
                            {  
                                if (cnt.GetMetaData("HostLink") != null)  
                                {  
                                    lnk.NavigateUrl = cnt.GetMetaData("HostLink").ToString();  
                                }  
     
                            }     
                              
                        }     
                    }     
                }  
                  
            }  
Register for webinar
5 posts, 1 answered