Sitefinity 5.4: Filter Module Builder Content Items by Choices Field
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
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"
));
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());