+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Filtering Dynamic Module by category

Filtering Dynamic Module by category

4 posts, 0 answered
  1. michaelroper
    michaelroper avatar
    18 posts
    Registered:
    25 Jul 2012
    07 Aug 2012
    Link to this post
    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!
  2. michaelroper
    michaelroper avatar
    18 posts
    Registered:
    25 Jul 2012
    08 Aug 2012
    Link to this post
    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!
  3. no longer at company
    no longer at company avatar
    2 posts
    Registered:
    14 Sep 2010
    01 Jul 2013 in reply to michaelroper
    Link to this post
    i had the same problem, i needed to add this to my code:
    using System.Linq;
  4. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    06 Dec 2016
    02 Jul 2013
    Link to this post
    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
4 posts, 0 answered