Filtering Dynamic Module by category
Hi guys, I'm banging my head against a wall on this one, and desperately/urgently need some help...
I have a Module Builder module, which has a category field called 'productcategories' that i want to filter by when i display the items. Here is my code-behind:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Telerik.Sitefinity.Model;using Telerik.Sitefinity.Model.ContentLinks;using Telerik.Sitefinity.Modules.Libraries;using Telerik.Sitefinity.DynamicModules;using Telerik.Sitefinity.Data.Linq.Dynamic;using Telerik.Sitefinity.DynamicTypes.Model;using Telerik.Sitefinity.DynamicModules.Model;using Telerik.Sitefinity.Utilities.TypeConverters;using Telerik.Sitefinity.GenericContent.Model;using Telerik.Sitefinity.Taxonomies;using Telerik.Sitefinity.Taxonomies.Model;namespace SitefinityWebApp public partial class ProductCategoryItemList : System.Web.UI.UserControl private string _productCategory; public string ProductCategory get return _productCategory; set _productCategory = value; protected void Page_Load(object sender, EventArgs e) //Get the GUID of the desired category var taxaManager = TaxonomyManager.GetManager(); var ProductCategoryId = taxaManager.GetTaxa<HierarchicalTaxon>().Where(t => t.Name == ProductCategory).SingleOrDefault().Id; DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(); Type productType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Products.Product"); // Fetch a collection of product items var productCollection = dynamicModuleManager.GetDataItems(productType).Where( // filter them by the chosen category i => i.GetValue<TrackedList<Guid>>("productcategories").Contains(ProductCategoryId) // only get the "live" and "visible" items. && i.Status == ContentLifecycleStatus.Live && i.Visible == true ); // Bind the collection of Products items to the Repeater ProductItemsList.DataSource = productCollection; ProductItemsList.DataBind();
|
After giving up for the night, and a bit more hair-pulling this morning, it appears that I was missing a using Telerik.OpenAccess directive.
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();Type productType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Products.Product");var productCollection = dynamicModuleManager.GetDataItems(productType);if (this.ProductCategory != null) Guid ProductCategoryGuid = new Guid(ProductCategory); // filtered the items by category productCollection = productCollection.Where(i => i.GetValue<TrackedList<Guid>>("productcategories").Contains(ProductCategoryGuid));// filter the filtered items by what is live and visibleproductCollection = productCollection.Where(i => i.Status == ContentLifecycleStatus.Live && i.Visible == true);// Bind the collection of Products items to the RepeaterProductItemsList.DataSource = productCollection;ProductItemsList.DataBind();ProductItemsNav.DataSource = productCollection;ProductItemsNav.DataBind();i had the same problem, i needed to add this to my code:
using System.Linq;
Hi all,
Thank you for sharing your solutions with the community.
Regards,
Stefani Tacheva
Telerik