Filtering Dynamic Module by category

Posted by Community Admin on 04-Aug-2018 13:45

Filtering Dynamic Module by category

All Replies

Posted by Community Admin on 07-Aug-2012 00:00

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();
 
        
    

The error is get is the following:

Compiler Error Message: CS1593: Delegate 'System.Func<Telerik.Sitefinity.DynamicModules.Model.DynamicContent,int,bool>' does not take 1 arguments
error CS1593: Delegate 'System.Func<Telerik.Sitefinity.DynamicModules.Model.DynamicContent,int,bool>' does not take 1 arguments
error CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type
error CS0246: The type or namespace name 'TrackedList' could not be found (are you missing a using directive or an assembly reference?)



Ive found someone else that had the same error in the forums, but the solution given there is pretty much exactly what I'm doing, and it's not working! :(

Am I missing something really stupid? please halp!

Posted by Community Admin on 08-Aug-2012 00:00

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.

Searching the taxonomies was also giving me grief, so I resorted to entering the category GUID in the widget, and just use that directly, as per below...

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 visible
productCollection = productCollection.Where(i => i.Status == ContentLifecycleStatus.Live && i.Visible == true);
 
// Bind the collection of Products items to the Repeater
ProductItemsList.DataSource = productCollection;
ProductItemsList.DataBind();
 
ProductItemsNav.DataSource = productCollection;
ProductItemsNav.DataBind();

any improvements on that are welcome!

Posted by Community Admin on 01-Jul-2013 00:00

i had the same problem, i needed to add this to my code:
using System.Linq;

Posted by Community Admin on 02-Jul-2013 00:00

Hi all,

Thank you for sharing your solutions with the community.

Regards,
Stefani Tacheva
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 Public Issue Tracking system and vote to affect the priority of the items

This thread is closed