Get a random selection from a dynamic module
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; 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