Sitefinity 5.4: Filter Module Builder Content Items by Choi

Posted by Community Admin on 04-Aug-2018 15:00

Sitefinity 5.4: Filter Module Builder Content Items by Choices Field

All Replies

Posted by Community Admin on 21-Feb-2013 00:00

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

 

 

Posted by Community Admin on 26-Feb-2013 00:00

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

Posted by Community Admin on 27-Feb-2013 00:00

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

This thread is closed