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

Forums / Developing with Sitefinity / Sitefinity 5.4: Filter Module Builder Content Items by Choices Field

Sitefinity 5.4: Filter Module Builder Content Items by Choices Field

3 posts, 0 answered
  1. Randy Garza
    Randy Garza avatar
    19 posts
    Registered:
    24 Feb 2012
    21 Feb 2013
    Link to this post

    Hi,

    I've created a new custom module in Sitefinity 5.4 with a Choice field.  In code I am trying to filter the collection of content items using a value that is available in the choice field.  Prior to version 5.4 I would use the following code to filter results:

    var myItems = dynamicModuleManager.GetDataItems(myDynamicType)
     .Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true && i.FieldValue<String[]>("MyChoiceField").Equals("FilterValue")).ToList();

    When I use the same code in 5.4 I am getting the following error:

    Database mapped field uses different type 'System.String'.
    Parameter name: methodCallExpression
    Actual value was re-i.FieldValue("MyChoiceField").

    What is the correct method of filtering a collection of items using a Choices field?

     

    Thanks for the help,

    Randy

     

     

  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    18 Aug 2017
    26 Feb 2013
    Link to this post
    Hi,

    I believe the reason why you don't get the correct items lay in the fact that you properly get the choices of the choiceField as an array, but the you compare the array to a String, which will not return the correct items. I would recommend you to try using Contains instead, for example:

    DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
               var item = dynamicModuleManager.GetDataItems(typeof(DynamicModuleManager)).
                   Where(i => i.GetValue<string[]>("MyChoiceField").Contains("option1"));


    Regards,
    Stanislav Velikov
    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
  3. Randy Garza
    Randy Garza avatar
    19 posts
    Registered:
    24 Feb 2012
    27 Feb 2013
    Link to this post

    Here is the answer from Support.  This is for Sitefinity version 5.4:

    var mngr = DynamicModuleManager.GetManager();
             //for dropdown (single item)
               mngr.GetDataItems(typeof(myType)).Where(i => i.GetValue<ChoiceOption>("test").PersistedValue == "option");
      
               // for multiple items
                
               mngr.GetDataItems(typeof(myType)).Where(i => i.GetValue<ChoiceOption[]>("test").Where( ch => ch.PersistedValue == "option").Any());

3 posts, 0 answered