More in this section

Forums / Developing with Sitefinity / Generic Content based Module - Display Random Items

Generic Content based Module - Display Random Items

2 posts, 0 answered
  1. Brook
    Brook avatar
    14 posts
    Registered:
    27 Oct 2009
    11 Dec 2008
    Link to this post
    I was very excited that I was able to adapt the product module example to create my own testimonials module, used the In the mean time series.  I want and like the current functionality but want to create on more control that would allow me to select the category and specifiy the sort, but pull a specified number of random items rather than a full list.  I still want to be able to show the limited word summary and have the links to the full items etc... just want to pull a random number of items to show.  My brain is on overload just getting to this point, any guidance on accomplishing this would be great!
  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    12 Dec 2008
    Link to this post
    Hi Brook,

    You need to create a control which extends from ContentView base class and then override CreateDataSource method. Here is a sample implementation:

    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.WebControls; 
    using System.Collections; 
    using System.Collections.Generic; 
    using Telerik.Cms.Engine; 
     
    namespace Telerik.Cms.Engine.WebControls 
        public class ContentViewExt : ContentView 
        { 
            private bool useRandom; 
            private int maxCount; 
     
            public virtual bool UseRandomTestimonials 
            { 
                get 
                { 
                    return useRandom; 
                } 
                set 
                { 
                    useRandom = value; 
                } 
            } 
     
            public virtual int MaxCount 
            { 
                get 
                { 
                    return maxCount; 
                } 
                set 
                { 
                    maxCount = value; 
                } 
            } 
     
            protected override IList CreateDataSource() 
            { 
                IList dataSource = base.CreateDataSource(); 
     
                if (this.UseRandomTestimonials && this.BehaviorMode != BehaviorModes.Detail && dataSource.Count > 0 && this.MaxCount > 0) 
                { 
                    IList list = new List<IContent>(); 
                    Random random = new Random(); 
                    IContent cnt = null
     
                    for (int i = 1; i <= this.MaxCount; i++) 
                    { 
                        cnt = dataSource[random.Next(0, dataSource.Count)] as IContent; 
                        if (cnt != null && !list.Contains(cnt)) 
                        { 
                            list.Add(cnt); 
                        } 
                    } 
     
                    dataSource = list; 
                } 
     
                return dataSource; 
            } 
        } 

    Do let us know if this helps.

    Regards,
    Pepi
    the Telerik team

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