Get dynamic modul entries in random order

Posted by Community Admin on 04-Aug-2018 12:39

Get dynamic modul entries in random order

All Replies

Posted by Community Admin on 09-May-2014 00:00

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;
         

Posted by Community Admin on 12-May-2014 00:00

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
 

Posted by Community Admin on 12-May-2014 00:00

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.

This thread is closed