A RadNewsRotator widget that show the news without relation betwwen news and images - General Discussions - General Discussions - Progress Community

A RadNewsRotator widget that show the news without relation betwwen news and images

 General Discussions

A RadNewsRotator widget that show the news without relation betwwen news and images

  • A RadNewsRotator widget that show the news without relation betwwen news and images
  • Hi, actually i try modify the radNewsRotator control of this example: http://bit.ly/fcMIuR , but the code-behind is complex 
    I want to show the news without the relation between the title of the news and the title of an image. Could you help me please?
  • Hi Luis,

    You can get only the news items using the code below

    App.WorkWith().NewsItems().Where(n => n.Status == ContentLifecycleStatus.Live).Get().Take(this.NewsLimit).ToList()

    Then you can pass the list as a datasource of the rotator control.

    All the best,
    Ivan Dimitrov
    the Telerik team
  • I tried with the code mentioned before but an error appeared 'Exception Details: System.NullReferenceException: Object reference not set to aninstance of an object.' in 
    Line38: NewsItem newsItem = (NewsItem)TypeDescriptor.GetProperties(e.Item.DataItem)["NewsItem"].GetValue(e.Item.DataItem);
    i searched in the methods of the API of the news module, but I did not work properly. Could you help me please?

  • Hi Luis,

    Could you check whether e.Item.DataItem is null. You are getting null reference exception.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
  • Hi, i don't found the code line in that the list took null values. I attach my code. Any suggestion?



        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        using Telerik.Sitefinity.News.Model;
        using System.ComponentModel;
        using Telerik.Web.UI;
        using Telerik.Sitefinity.GenericContent.Model;
        using Telerik.Sitefinity.Web.DataResolving;
        using Telerik.Sitefinity.Pages.Model;
        using Telerik.Sitefinity;
        using Telerik.Sitefinity.Modules.News.Web.UI;
        using Telerik.Sitefinity.Modules.News;




        public partial class User_Controls_NewsRotator : System.Web.UI.UserControl
        
            protected void Page_Load(object sender, EventArgs e)
            
                NewsManager Manager = NewsManager.GetManager();


                this.RadRotator1.DataSource = Manager.GetNewsItems()
                    .Where(n => n.Status == ContentLifecycleStatus.Live).ToList();
     
                RadRotator1.ItemDataBound += new RadRotatorEventHandler(RadRotator1_ItemDataBound);
                RadRotator1.DataBind();
            


            void RadRotator1_ItemDataBound(object sender, Telerik.Web.UI.RadRotatorEventArgs e)
            
                var link = e.Item.FindControl("newsLink") as HyperLink;
                var title = e.Item.FindControl("newsTitle") as Label;
                var text = e.Item.FindControl("newsText") as Label;


                NewsItem newsItem = (NewsItem)TypeDescriptor.GetProperties(e.Item.DataItem)["NewsItem"].GetValue(e.Item.DataItem);        
            
                if (title != null) title.Text = newsItem.Title;
                if (text != null) text.Text = newsItem.Summary;
                if (link != null && TargetNewsPage != null) link.NavigateUrl = DataResolver.Resolve(newsItem, "URL", null, TargetNewsPage.Id.ToString());
            


            protected PageNode TargetNewsPage
            
                get
                
                    if (this.targetNewsPage == null)
                    
                        this.targetNewsPage = App.WorkWith().Pages()
                                    .Where(p => p.Page != null &&
                                                p.Page.Controls.Where(c => c.ObjectType.StartsWith(typeof(NewsView).FullName)).Count() > 0)
                                    .Get().FirstOrDefault();


                     //it can still be null in case there is no page with a NewsView on it
                    return this.targetNewsPage;
                


            


            private PageNode targetNewsPage;


            public virtual int NewsLimit
            
                get
                
                    return this.newsLimit;
                
                set
                
                    this.newsLimit = value;
                
            
            private int newsLimit = 8;
        

  • Hello Luis,

    I am not sure what you are trying to do with this line in your code


    NewsItem newsItem = (NewsItem)TypeDescriptor.GetProperties(e.Item.DataItem)["NewsItem"].GetValue(e.Item.DataItem);

    where the error is actually thrown.

    TypeDescriptor.GetProperties returns the collection of properties on a component or type. There is no property NewsItem which you pass as a key.
    The data item of the repeater is NewsItem so you can have only this

    var newsItem = e.Item.DataItem as NewsItem;

    If the e.Item.DataItem is null then the repeater datasource is empty and there are no items in it.

    Regards,
    Ivan Dimitrov
    the Telerik team
  • What he is trying to do in that line of code is what Sitefinity tells us to do. That line of code, in fact that whole method is from your OWN sitefinity documentation:

    docs.sitefinity.com/newsrotator-widget-implement-the-features-of-the-user-control

    I've been having a heck of a time getting that widget to work as well, and have reported errors in this documentation before.  I really hope you will correct it.