5

For this below logic, how to write a CAML query? Kindly guide me.

Query Logic:

where department ='HR' And (Team='A' Or Team ='C' Or Team ='D')

Here "department" and "Team" is column name and ("HR","A","C","D") is value.

2 Answers 2

9

Considered that Team and Department columns are text column.

"<Query><Where>"+
        "<And>"+
            "<Or>"+
                "<Or>"+
                     "<Eq><FieldRef Name='Team' /><Value Type='Text'>A</Value></Eq>"+
                     "<Eq><FieldRef Name='Team' /><Value Type='Text'>C</Value></Eq>"+
                "</Or>"+
                "<Eq><FieldRef Name='Team' /><Value Type='Text'>D</Value></Eq>"+
            "</Or>"+
            "<Eq><FieldRef Name='department' /><Value Type='Text'>HR</Value></Eq>"+
        "</And>"+               
    "</Where></Query>"

Check my answer on how to write nested query.

1
  • It is good to use free query builder tools, however if you understand the logic, it will be forever and you will not depend on any tools ever.
    – Gaurravs
    Commented Jun 28, 2019 at 7:06
4

You can easily create CAML queries if you use any of these s/w these are free to use and available in both install/offline packages. However, CAML query for your scenario would look like this.

Free CAML query builder: BIWUG CAML Designer for SP 2016/13/10 to O365

   <Where>
      <And>
         <Or>
            <Or>
               <Eq>
                  <FieldRef Name='Team/>
                  <Value Type='Choice'>A</Value>
               </Eq>
               <Eq>
                  <FieldRef Name='Team' />
                  <Value Type='Choice'>B</Value>
               </Eq>
            </Or>
            <Eq>
               <FieldRef Name='Team' />
               <Value Type='Choice'>C</Value>
            </Eq>
         </Or>
         <Eq>
            <FieldRef Name='Department' />
            <Value Type='Choice'>HR</Value>
         </Eq>
      </And>
   </Where>
1
  • Your query logic is incorrect, you say Department is 'HR' OR Team = 'A' OR Team = .... Department and any Team value should be in an AND clause.
    – Abbas
    Commented Jan 2, 2018 at 10:49

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.