Get a random selection from a dynamic module

Posted by Community Admin on 04-Aug-2018 14:27

Get a random selection from a dynamic module

All Replies

Posted by Community Admin on 15-Aug-2014 00:00

I once in a while post stuff that was helpful to me and I hope it could be to others.

This is how I retrieve a certain number of records from my dynamic modules and those random.

 I get the collection as per generated code reference but add

.Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true);

So I dont get the master and deleted versions.

Then take myCollection and  add  .AsEnumerable().OrderBy(x => Guid.NewGuid()).Take(3);

To have it random and then select 3 in my example.

Markus

 

protected void Page_Load(object sender, EventArgs e)
       
           var myCollection = RetrieveCollectionOfBranchenbilds();
           var orderedCollection = myCollection.AsEnumerable().OrderBy(x => Guid.NewGuid()).Take(3);
                   RadGrid2.DataSource = orderedCollection;
           RadGrid2.DataBind();
       
 
       public IQueryable<DynamicContent> RetrieveCollectionOfBranchenbilds()
       
           // Set the provider name for the DynamicModuleManager here. All available providers are listed in
           // Administration -> Settings -> Advanced -> DynamicModules -> Providers
           var providerName = String.Empty;
           DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
           Type branchenbildType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Bannerbilder.Branchenbild");
            
          // This is how we get the collection of Branchenbild items
           var myCollection = dynamicModuleManager.GetDataItems(branchenbildType).Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true);
           // At this point myCollection contains the items from type branchenbildType
           return myCollection;
       

Posted by Community Admin on 20-Aug-2014 00:00

Hi Markus,

Thank you for sharing this. You contribute to the community is always welcome. As a token of appreciation I have increased your Telerik points.

Regards,
Pavel Benov
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
 

This thread is closed