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

Forums / Developing with Sitefinity / Filter Dynamic Module content by Category (HierarchicalTaxon)

Filter Dynamic Module content by Category (HierarchicalTaxon)

10 posts, 1 answered
  1. Christian
    Christian avatar
    5 posts
    Registered:
    22 Aug 2011
    12 Jan 2012
    Link to this post
    Hi,

    We have used the new Module Builder in Sitefinity 4.4 to make a module that stores Contacts for a business website. Each contact has an associated Category (HierarchicalTaxon) which we have used to associate a location with the contact.

    The Module Builder provides code examples for fetching and filtering a Contact by name etc. What we would like to do is filter a Contact by Category (HierarchicalTaxon). I have the Category GUID stored in a cookie.

    1.HttpCookie LocationCategoryCookie = Request.Cookies["location_category"];
    2.location_category = Server.HtmlEncode(LocationCategoryCookie.Value);
    3. 
    4.DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
    5.Type contactType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Contacts.Contact");
    6. var myFilteredCollection = dynamicModuleManager.GetDataItems(contactType).Where("The code we need is here... get contact by location_category");
     
    Any help would be much appreciated.

    Christian
  2. Dimitar
    Dimitar avatar
    1 posts
    Registered:
    12 Jan 2012
    12 Jan 2012
    Link to this post
    var myFilteredCollection = dynamicModuleManager.GetDataItems(contactType).
    Where(
    item => item.getValue<TrackedList<Guid>>("Category").Contains(category.Id));
  3. Christian
    Christian avatar
    5 posts
    Registered:
    22 Aug 2011
    13 Jan 2012
    Link to this post
    Thanks for the response, this was similar to my first attempt, but the following errors appear...

    Error   1   Cannot convert lambda expression to type 'string' because it is not a delegate type
    Error   2   Delegate 'System.Func<Telerik.Sitefinity.DynamicModules.Model.DynamicContent,int,bool>' does not take 1 arguments
    Error   3   'Telerik.Sitefinity.DynamicModules.Model.DynamicContent' does not contain a definition for 'getValue' and no extension method 'getValue' accepting a first argument of type 'Telerik.Sitefinity.DynamicModules.Model.DynamicContent' could be found (are you missing a using directive or an assembly reference?)
    Error   4   The name 'category' does not exist in the current context

  4. Christian
    Christian avatar
    5 posts
    Registered:
    22 Aug 2011
    16 Jan 2012
    Link to this post
    anyone?
  5. Christian
    Christian avatar
    5 posts
    Registered:
    22 Aug 2011
    16 Jan 2012
    Link to this post
    ok, tried a different method today to no avail...

    01.protected void Page_Load(object sender, EventArgs e) {
    02.  RetrieveContactThroughFiltering();
    03.}
    04. 
    05.public void RetrieveContactThroughFiltering() {
    06. 
    07.  HttpCookie LocationCategoryCookie = Request.Cookies["location_category"];
    08.  System.Guid location_category = new System.Guid(LocationCategoryCookie.Value);
    09.  OpenAccessDynamicModuleProvider dynamicModuleManager = new OpenAccessDynamicModuleProvider();
    10. 
    11.  var taxonomyManager = TaxonomyManager.GetManager();
    12.  var taxonGuidId = new Guid(LocationCategoryCookie.Value);
    13.  ITaxon taxon = taxonomyManager.GetTaxon(taxonGuidId);
    14. 
    15.  string itemTypeName = "Telerik.Sitefinity.DynamicTypes.Model.OurContacts.Contact";
    16.  Type itemType = TypeResolutionService.ResolveType(itemTypeName);
    17.  var manager = ManagerBase.GetMappedManager(itemType, "");
    18. 
    19.  GetItems(taxon, dynamicModuleManager, itemType);
    20. 
    21.}
    22. 
    23.private TaxonomyPropertyDescriptor GetPropertyDescriptor(Type itemType, ITaxon taxon) {
    24.  return TaxonomyManager.GetPropertyDescriptor(itemType, taxon);
    25.}
    26. 
    27.private IEnumerable GetItems(ITaxon taxon, OpenAccessDynamicModuleProvider contentProvider, Type itemType) {
    28.  TaxonomyPropertyDescriptor prop = GetPropertyDescriptor(itemType, taxon);
    29.  int? totalCount = 1;
    30.  var items = contentProvider.GetItemsByTaxon(taxon.Id, prop.MetaField.IsSingleTaxon, prop.Name, itemType, string.Empty, string.Empty, 0, 100, ref totalCount);
    31.  return items;
    32.}


    With this attempt I get an error stating...

    Value cannot be null.
    Parameter name: typeRequested

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentNullException: Value cannot be null.
    Parameter name: typeRequested

    Source Error:

    Line 70:       TaxonomyPropertyDescriptor prop = GetPropertyDescriptor(itemType, taxon);
    Line 71:       int? totalCount = 1;
    Line 72:       var items = contentProvider.GetItemsByTaxon(taxon.Id, prop.MetaField.IsSingleTaxon, prop.Name, itemType, string.Empty, string.Empty, 0, 100, ref totalCount);
    Line 73:       return items;
    Line 74:     }
  6. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    19 Sep 2016
    16 Jan 2012
    Link to this post
    Hi Christian,

    If your dynamic module contains a metafield of type classification (e.g. Category) you can obtain its value using our extension method GetValue(). Here's a sample how to retrieve all published dynamic module items that have the "Cat1" category assigned to them:

    var manager = TaxonomyManager.GetManager();
                //Get the GUID of the desired category
                var myCategoryId = manager.GetTaxa<HierarchicalTaxon>().Where(t => t.Name == "Cat1").SingleOrDefault().Id;
                DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
                Type dynamicmoduleitemType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MyDynamicModule.Dynamicmoduleitem");
                var myItems = dynamicModuleManager.GetDataItems(dynamicmoduleitemType)
                                                    .Where(d => d.GetValue<TrackedList<Guid>>("Category").Contains(myCategoryId)
                                                                && d.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);

    Greetings,
    Boyan Barnev
    the Telerik team
    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
    Answered
  7. Christian
    Christian avatar
    5 posts
    Registered:
    22 Aug 2011
    17 Jan 2012
    Link to this post
    Works a treat :)

    This is our first Sitefinity site, the help and info is much appreciated.
  8. no longer at company
    no longer at company avatar
    2 posts
    Registered:
    14 Sep 2010
    01 Jul 2013 in reply to Christian
    Link to this post
    i had the same problem, i needed to add this to my code:
    using System.Linq;
  9. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    09 Sep 2016
    02 Jul 2013
    Link to this post
    Hello,

    Thank you for sharing your solution 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
  10. Nick
    Nick avatar
    113 posts
    Registered:
    21 Oct 2008
    03 Jul 2014
    Link to this post

    Hi,

     Thanks for sharing this, I wanted do a filter and it worked fine.

    Filter List based on Category 

10 posts, 1 answered