Search code examples
sharepointcaml

CAML Query with multiple conditions


I'm trying to add another OR condition in my CAML Query below, but nothing seems to be working. Can someone help? Please see attached picture of the library. I am trying to get the highlighted data. Here is the CAML query. Library Name and Level stays the same. I need to grab the 1st row, 3rd and 4th row. The first attempt I am trying to get the exact person (Marshall, Kim), then I am saying give me data where Line=Pure and Any Local is Yes (not working but it should give me Sandoz, Newman), then also give me where Any Line is yes and Any Local is Yes for Packaging with Level A. (Howey, Laurel).

enter image description here

 <Query>
  <ViewFields>
    <FieldRef Name="AssignedTo" />
  </ViewFields>
  <Where>
      <And>
         <Eq>
            <FieldRef Name='Library' />
            <Value Type='Lookup'>Packaging</Value>
         </Eq>
         <And>
            <Eq>
               <FieldRef Name='Level' />
               <Value Type='Choice'>A</Value>
            </Eq>
            <And>
               <Eq>
                  <FieldRef Name='Line' />
                  <Value Type='Lookup'>PURE</Value>
               </Eq>
               <Eq>
                  <FieldRef Name='Place' />
                  <Value Type='Lookup'>Vincente, New Port, CA</Value>
               </Eq>
            </And>
         </And>
 </And>
 <And>
<Or>
    <And>
      <And>
        <And>
          <Eq>
            <FieldRef Name='Library' />
            <Value Type='Lookup'>Packaging</Value>
          </Eq>
          <Eq>
            <FieldRef Name='Level' />
            <Value Type='Choice'>A</Value>
          </Eq>
        </And>
        <Eq>
          <FieldRef Name='Line' />
          <Value Type='Lookup'>PURE</Value>
        </Eq>
      </And>
      <Eq>
        <FieldRef Name='AnyPlace' />
        <Value Type='Boolean'>1</Value>
      </Eq>
    </And>
</Or>
      </And>

  </Where>
</Query>

Solution

  • I got some help from Gaurav. https://www.gaurravs.com/post/understanding-dynamic-queries

    Here is the final CAML that helped.

    <Query>
    <ViewFields>
        <FieldRef Name="AssignedTo" />
    </ViewFields>
    <Where>
        <And>
            <Eq>
                <FieldRef Name='Library' />
                <Value Type='Lookup'>Packaging</Value>
            </Eq>
            <And>
                <Eq>
                    <FieldRef Name='Level' />
                    <Value Type='Choice'>A</Value>
                </Eq>            
                <Or>
                    <Eq>
                        <FieldRef Name='Line' />
                        <Value Type='Lookup'>PURE</Value>
                    </Eq>
                    <And>
                        <Eq>
                            <FieldRef Name='AnyLine' />
                            <Value Type='Boolean'>1</Value>
                        </Eq> 
                        <Eq>
                            <FieldRef Name='AnyLocal' />
                            <Value Type='Boolean'>1</Value>
                        </Eq>
                    </And>
                </Or>
            </And>
        </And>
    </Where>