3

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"

1 Answer 1

3

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.

2
  • This will be complicated if i have to add Query like this . I thought i can pass comma seperated string
    – Happy
    Feb 5, 2016 at 7:34
  • Spelling mistake hence was not getting records. But really i didnt liked this idea of building <contains> tag. I have 4 checkboxes, I have to find combinations of what is checked and then build this caml query
    – Happy
    Feb 5, 2016 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.