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

Forums / Developing with Sitefinity / Get dynamic modul entries in random order

Get dynamic modul entries in random order

3 posts, 0 answered
  1. Markus
    Markus avatar
    2763 posts
    Registered:
    25 Nov 2005
    09 May 2014
    Link to this post

    I would like to have a random order of content from my dynamic module.

     .OrderBy(X => random.next()) 

    .OrderBy(x => Guid.NewGuid())

     dont seem to be working. 

    Markus

     

    public IQueryable<Telerik.Sitefinity.DynamicModules.Model.DynamicContent> GetDataItems()
           {
               DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
               Type videoType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.VideoverwaltungHomepage.Video");
                Random random = new Random();
               // Fetch a collection of "live" and "visible" video items.
               var myCollection = dynamicModuleManager.GetDataItems(videoType)
                   .Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true);
               //  .Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true).OrderBy(x => random.Next());
               //  .Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true).OrderBy(x => Guid.NewGuid()) ;
               //  Literal1.Text += "Collection count: " + myCollection.Count().ToString() + "<br />";
             
               return myCollection;
             }

  2. Georgi Georgiev
    Georgi Georgiev avatar
    10 posts
    Registered:
    27 Apr 2016
    12 May 2014
    Link to this post

    Hi Markus,

    Thank you for contacting Sitefinity Support team.

    I created a test page where I displayed the items in the way they come from the query and randomized and it seems to work. I just added .AsEnumerable before Ordering them.

    Please review the code below:

    StringBuilder sb = new StringBuilder();
    StringBuilder sb1 = new StringBuilder();
     
     var mycollection1 = manager.GetDataItems(type).Where(i => i.GetValue<string>("Title").Contains("t"));
     
         foreach (var item in mycollection1)
     
                {
                    sb.Append(String.Format("{0} - Status - {1}", item.Id, item.Status));
                }
     
                var orderedCollection = manager.GetDataItems(type).Where(i => i.GetValue<string>("Title").Contains("t")).AsEnumerable().OrderBy(x => Guid.NewGuid());
     
                foreach (var item in orderedCollection)
     
                {
                    sb1.Append(String.Format("{0} - Status - {1}", item.Id, item.Status));
                }
     
                this.Label1.Text = sb.ToString();
                this.Label2.Text = sb1.ToString();


    Regards,
    Georgi Georgiev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  3. Markus
    Markus avatar
    2763 posts
    Registered:
    25 Nov 2005
    12 May 2014 in reply to Georgi Georgiev
    Link to this post

    Dear Georgi

    Thank's for your feedback. Its working like this. 

    Markus

    PS: If I cache the page in SF it would load a different order for each browser/client but reload of course did not bring new order. Disabling page cache would make it load different order every time.

3 posts, 0 answered