More in this section

Forums / Developing with Sitefinity / Get a random selection from a dynamic module

Get a random selection from a dynamic module

2 posts, 0 answered
  1. Markus
    Markus avatar
    2763 posts
    25 Nov 2005
    15 Aug 2014
    Link to this post

    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.



    protected void Page_Load(object sender, EventArgs e)
               var myCollection = RetrieveCollectionOfBranchenbilds();
               var orderedCollection = myCollection.AsEnumerable().OrderBy(x => Guid.NewGuid()).Take(3);
                       RadGrid2.DataSource = orderedCollection;
           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;

  2. Pavel Benov
    Pavel Benov avatar
    341 posts
    14 Mar 2016
    20 Aug 2014
    Link to this post
    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.

    Pavel Benov
    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
2 posts, 0 answered