OrderBy issue when retrieving child items in dynamic data mo

Posted by Community Admin on 04-Aug-2018 20:05

OrderBy issue when retrieving child items in dynamic data module

All Replies

Posted by Community Admin on 27-Mar-2015 00:00

We are running Sitefinity 7.3 and I have created a Module called StoreCategories in Module Builder. Each StoreCategory has children of type StoreProducts. I am using the following function to successfully retrieve the StoreProducts for a given StoreCategory.

 

public IQueryable<DynamicContent> RetrieveStoreProducts(DynamicContent Category)
        
            var providerName = String.Empty;
 
            DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
            IQueryable<DynamicContent> products = dynamicModuleManager.GetChildItems(Category).Where(i => i.Status == ContentLifecycleStatus.Live && i.Visible == true);
            return products;
        

My question is that I need to order the results by a field called weight that was defined in module builder.

I have tried something like this

IQueryable<DynamicContent> products = dynamicModuleManager.GetChildItems(Category).Where(i => i.Status == ContentLifecycleStatus.Live && i.Visible == true).OrderyBy("Weight");

but I am getting an error stating "No 'Weight' member found in type 'Telerik.Sitefinity.DynamicModules.Model.DynamicContent'" The weight field is definitely defined in Module builder for StoreProducts but I suspect that I am missing something in my logic. Any help would be greatly appreciated.

 Thank you.

 

Posted by Community Admin on 01-Apr-2015 00:00

Hello Chris,

I have answered you in the ticket that you have opened for this issue. Feel free to share the answer with our community.

Regards,
Kaloyan
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 01-Apr-2015 00:00

So it turns out that the answer was to not use the dynamicModuleManager.GetChildItems(Category) approach to retrieve the StoreProduct objects but rather to query the StoreProducts directly and specify the parent's id in a Where clause. This should allow you to use OrderBy. See the amended method below which works correctly.

 

public IQueryable<DynamicContent> RetrieveStoreProducts(DynamicContent Category)
        
            var providerName = String.Empty;
 
            DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
 
            Type storeProductType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.StoreCategories.StoreProduct");
 
            // This is how we get the collection of Store Product items
            IQueryable<DynamicContent> storeProducts = dynamicModuleManager.GetDataItems(storeProductType)
                                                    .Where(a => a.SystemParentId == Category.Id)
                                                    .Where(i => i.Status == ContentLifecycleStatus.Live && i.Visible == true)
                                                    .OrderBy(o => o.GetValue<decimal?>("Weight"));
 
            return storeProducts;
        

This thread is closed