OrderBy issue when retrieving child items in dynamic data module
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.
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
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;