filter on multichoice column
-
07-10-2020 - |
Question
In my list i have multichoice column... How to filter on it programatically.. I know to filter based on text column like below
SPList oList = oWebsiteRoot.Lists["mylist"]; // list_name
SPQuery query = new SPQuery();
query.Query = "<Where><And><Lt><FieldRef Name='ID'/>" + "<Value Type='Integer'>" + itemIndex + "</Value></Lt><Eq><FieldRef Name='Status'/>" +
"<Value Type='Text'>Approved</Value></Eq></And></Where><OrderBy><FieldRef Name='ID' Ascending='False'></FieldRef></OrderBy>";
SPListItemCollection items = null;
items = oList.GetItems(query);
return items;
But how to filter on multichoice column passing it comma seperate string .
I also tried using U2uCamlQuery Builder. But when i try to add filter it throws error "value cannot be null".
Update1 I tried below Query now
query.Query = "<Where><Or><Contains><FieldRef Name ='ExtName'/><Value Type ='MultiChoice'>Test1</Value></Contains><Contains><FieldRef Name ='ExtName'/><Value Type ='MultiChoice'>Test2</Value></Contains></Or></Where> ";
But it gives only records from value "Test1" even though there are records With value "Test2"
Solution
Without fully knowing your list data structure, I'm thinking your query string should be formatted along the lines of :
<Where>
<Or>
<Contains>
<FieldRef Name='SeriousHarmQ1' />
<Value Type='MultiChoice'>Loss of consciousness</Value>
</Contains>
<Contains>
<FieldRef Name='SeriousHarmQ1' />
<Value Type='MultiChoice'>Amputation of a body part</Value>
</Contains>
</Or>
</Where>
As an alternative to U2UCamlQueryBuilder, I tend to use CAML Designer 2013, which works well for me.
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange