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

Forums / Developing with Sitefinity / Master / Detail News default to latest

Master / Detail News default to latest

13 posts, 0 answered
  1. Nathan Davis
    Nathan Davis avatar
    5 posts
    Registered:
    13 Nov 2009
    13 Nov 2009
    Link to this post
    I have two NewsView controls on a page. One has behavior Master and the other Detail. This works as expected except that I can't seem to find a way to have the Detail control show the first news item (the latest) in the Master list when the user first browses to the page.

    The way it is now, the Detail control shows nothing when you first go to the page. This isn't very usable. 

    What is the best way to achieve my desired functionality?
    Thanks.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    15 Nov 2009
    Link to this post
    Hi Nathan Davis,

    The easiest way to achieve this task will be creating third control that will be used to show only the item you want - say the last published item. You need to create a simple template that your control will use and show the data from content item object. When you use NewsView and click on an item you make a post back and you are redirected to another url formed by urlRewriteFormat set in NewsProvider. When you are redirected to the new page you can hide the third control and show only NewsView that has been set to Details Behavior mode. The code below will gets only the last published item from your content list.

    NewsManager man = new NewsManager("News");
    IList listofAll = man.Content.GetContent("Publication_Date DESC");
    IContent cnt = listofAll[0] as IContent;

    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.
  3. Nathan Davis
    Nathan Davis avatar
    5 posts
    Registered:
    13 Nov 2009
    16 Nov 2009
    Link to this post
    Should the third control inherit from NewsView or some other built-in control? If so, what method(s) need to be overridden?

    Also, what is the best way to check from a custom control if the current page load is the result of a redirect to a specific news item Detail view?
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    16 Nov 2009
    Link to this post
    Hello Nathan Davis,

    There are two options here:

    1. Use obsolete member called SelectedItemId and set the id to the desired news item.

    2. Create a custom control that inherits NewsView
     - override SingleItemTemplatePath
     - override ItemListTemplatePath
     - override CreateChildControls. Before calling the base get the item id , filter the list.

    Sample:

    protected override void CreateChildControls()
     {
          
     
         newsItemID = new Guid(Context.Request[ContentItemKey];
         if(itemId != Guid.Empty && SingleModeSupported)
         {
                ContentFilterBuilder filterBuilder = new ContentFilterBuilder(this);
               filterBuilder.AddFilter(new ContentFilterStatement("ID", itemId.ToString(),
                                                                        ContentFilter.Condition.Equal,
                                                                        ContentFilter.JoinType.And));
                     behaviorModeImpl = BehaviorModes.Detail;
                     this.CreateSingleContent();
                     Controls.Add(SingleContainer);
         }
          base.CreateChildControls();
          
     }


    Greetings,
    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. Nathan Davis
    Nathan Davis avatar
    5 posts
    Registered:
    13 Nov 2009
    16 Nov 2009
    Link to this post
    UPDATE: this issue is resolved. See the next reply for an update and the new issue I'm having.
    --------------------------------------------------------------------------------------------------------------------

    I have created a new control as follows:

    public class LatestNewsView : Telerik.News.WebControls.NewsView 
        { 
            protected override void CreateChildControls() 
            { 
                var itemId = new Guid(Context.Request[ContentItemKey]); 
                if (itemId == Guid.Empty && SingleModeSupported) 
                { 
                    var man = new NewsManager("News"); 
                    //get latest news item. 
                    IList listofAll = man.Content.GetContent(0, 1, "Publication_Date DESC"); 
                    var latestContent = listofAll[0] as IContent; 
                    if (latestContent != null
                    { 
                        var filterBuilder = new ContentFilterBuilder(this); 
                        filterBuilder.AddFilter(new ContentFilterStatement("ID", latestContent.ID.ToString(), 
                                                                           ContentFilter.Condition.Equal, 
                                                                           ContentFilter.JoinType.And)); 
                        this.BehaviorMode = BehaviorModes.Detail; 
                        this.CreateSingleContent(); 
                        Controls.Add(SingleContainer); 
                    } 
                } 
                base.CreateChildControls(); 
            } 
        } 

    My intent is to check if a ContentItemKey is not specified and if so just default to the latest News Item.

    This compiles and I am able to successfully upload the DLL into Sitefinity and add the control to my page. However, when I try to edit it I get the following Exception:
    -------------------------------------------------------------------------------------------------

    Value cannot be null or empty.
    Parameter name: handler

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.ArgumentException: Value cannot be null or empty.
    Parameter name: handler

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace: 

    [ArgumentException: Value cannot be null or empty.Parameter name: handler]   System.Web.UI.ScriptComponentDescriptor.AddEvent(String name, String handler) +322783   Telerik.Web.ScriptObjectBuilder.DescribeComponent(Object instance, ScriptComponentDescriptor descriptor, IUrlResolutionService urlResolver, IControlResolver controlResolver) +1030   Telerik.Web.UI.ControlItemContainer.DescribeComponent(ScriptComponentDescriptor descriptor) +63   Telerik.Web.UI.RadTabStrip.DescribeComponent(ScriptComponentDescriptor descriptor) +24   Telerik.Web.UI.RadDataBoundControl.Telerik.Web.IControl.DescribeComponent(ScriptComponentDescriptor descriptor) +10   Telerik.Web.UI.ScriptRegistrar.GetScriptDescriptors(Control control) +112   Telerik.Web.UI.RadDataBoundControl.GetScriptDescriptors() +4   Telerik.Web.UI.RadDataBoundControl.System.Web.UI.IScriptControl.GetScriptDescriptors() +10   System.Web.UI.ScriptControlManager.RegisterScriptDescriptors(IScriptControl scriptControl) +101   System.Web.UI.ScriptManager.RegisterScriptDescriptors(IScriptControl scriptControl) +21   Telerik.Web.UI.RadDataBoundControl.RegisterScriptDescriptors() +37   Telerik.Web.UI.RadDataBoundControl.Render(HtmlTextWriter writer) +104   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19   System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer) +10   System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +32   System.Web.UI.WebControls.CompositeControl.Render(HtmlTextWriter writer) +38   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19   System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +163   System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32   System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +51   System.Web.UI.Adapters.ControlAdapter.Render(HtmlTextWriter writer) +21   Telerik.Cms.Web.FormAdapter.Render(HtmlTextWriter writer) +142   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +8683001   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99   System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +40   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19   System.Web.UI.Page.Render(HtmlTextWriter writer) +29   Telerik.Web.UI.RadAjaxPage.Render(HtmlTextWriter writer) +43   Telerik.Cms.Web.CmsPageBase.Render(HtmlTextWriter writer) +4   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266


    Version Information: Microsoft .NET Framework Version:2.0.50727.4200; ASP.NET Version:2.0.50727.4016


    --------------------------------------------------------------------------------------------
    Any idea what is wrong with my control?
    Thanks
  6. Nathan Davis
    Nathan Davis avatar
    5 posts
    Registered:
    13 Nov 2009
    16 Nov 2009
    Link to this post
    OK, I have figured out the reason for the previous Exception (it was that the Guid constructor was being passed a null string). That is fixed now, but now I am getting another Exception.

    My current custom control code is the following:

    public class LatestNewsView : Telerik.News.WebControls.NewsView 
        protected override void CreateChildControls() 
        { 
            if (String.IsNullOrEmpty(Context.Request[ContentItemKey])) 
            { 
                if (SingleModeSupported) 
                { 
                    var man = new NewsManager("News"); 
                    //get latest news item. 
                    IList listofLatest = man.Content.GetContent(1, 1, "Publication_Date DESC"); 
                    var latestContent = listofLatest[0] as IContent; 
                    if (latestContent != null
                    { 
                        var filterBuilder = new ContentFilterBuilder(this); 
                        filterBuilder.AddFilter(new ContentFilterStatement("ID", latestContent.ID.ToString(), 
                                                                               ContentFilter.Condition.Equal, 
                                                                               ContentFilter.JoinType.And)); 
                        this.BehaviorMode = BehaviorModes.Detail; 
                        this.CreateSingleContent(); 
                        Controls.Add(SingleContainer); 
                    } 
                } 
            } 
            base.CreateChildControls(); 
        } 
     

    When I add this custom control to a page along with a NewsView with Behavior of Master, I get the following error when browsing to the page:
    ------------------------------------------------------------------------------------------

    Invalid content ID specified

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.ArgumentException: Invalid content ID specified

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace: 

    [ArgumentException: Invalid content ID specified]   Telerik.Cms.Engine.WebControls.ContentView.CreateSingleContent() +2946   imglobal.sitefinity.controls.LatestNewsView.CreateChildControls() +318   System.Web.UI.Control.EnsureChildControls() +87   System.Web.UI.Control.PreRenderRecursiveInternal() +44   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842


    Version Information: Microsoft .NET Framework Version:2.0.50727.4200; ASP.NET Version:2.0.50727.4016

    ------------------------------------------------------------------------------------------

    Thanks
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    16 Nov 2009
    Link to this post
    Hi Nathan Davis,

    The error says that the ID you pass is not a valid. The content.ID is a Guid. Try convert it to string and pass it to the filter, or directly pass it to the filter and cast it to string( as my initial sample shows).

    Sincerely yours,
    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.
  8. Nathan Davis
    Nathan Davis avatar
    5 posts
    Registered:
    13 Nov 2009
    16 Nov 2009
    Link to this post
    I am converting the Guid to a string (If I wasn't, I would get a compile error, not the runtime exception I shared above). Did you look at my code?

    latestContent.ID.ToString() 

    The exception is happening during the call to the CreateSingleContent() method.
    Any other ideas?
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    18 Nov 2009
    Link to this post
    Hi Nathan Davis,

    I sent a reply to your support request where I completed the code.

    All the best,
    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.
  10. David van Geel
    David van Geel avatar
    59 posts
    Registered:
    19 Aug 2009
    27 May 2010
    Link to this post

    Hi Ivan,

    Is it possible to add the completed code you mentioned to this thread? I am trying to do something similar and running into the same error.

     

    Regards,


    David

  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    27 May 2010
    Link to this post
    Hello David van Geel,

    Here is a similar sample

    class NewsViewControl : NewsView
    {
        
          
      
        public override string ItemListTemplatePath
        {
            get { return "~/Sitefinity/ControlTemplates/News/ListPageMaster.ascx"; }
        }
      
        public override string SingleItemTemplatePath
        {
            get { return "~/Sitefinity/ControlTemplates/News/ListPageDetails.ascx"; }
        }
      
      
        protected override void CreateChildControls()
        {
            // instance of NewsManager
            var man = new NewsManager("News");
            // get all items by Publication date and get the first coneten item
            var listofAll = man.Content.GetContent("Publication_Date DESC");
            var cnt = listofAll[0] as IContent;
             
            //set the item ID
            if (Context.Items.Contains(this.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]);
            else
            {
                itemId = cnt.ID;
            }
       
            if (this.itemId != Guid.Empty && SingleModeSupported)
            {
                ContentFilterBuilder filterBuilder = new ContentFilterBuilder(this);
                filterBuilder.AddFilter(new ContentFilterStatement("ID", itemId.ToString(),                              ContentFilter.Condition.Equal,
                this.CreateSingleContent();
                Controls.Add(SingleContainer);
            }
        }
    }


    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.
  12. David van Geel
    David van Geel avatar
    59 posts
    Registered:
    19 Aug 2009
    27 May 2010
    Link to this post
    Thanks, Ivan.

    Quick, to the point AND it solves my problem. :)

    Regards,


    David
  13. Tristan
    Tristan avatar
    22 posts
    Registered:
    30 Mar 2009
    25 Aug 2010
    Link to this post
    Thanks, This solved my problem too.

    Don't forget to add base.CreateChildControls();  to the end of the CreateChildControls method.
Register for webinar
13 posts, 0 answered