Properly Nesting Compound AND / OR Statements Using CAML

84 Views Asked by At

I am leveraging SPservices within a SharePoint 2013 environment to create filtered views unique to the current user. Effectively, when a user visits a page, they get a custom feed of documents that they have uploaded to a specific library.

I can successfully query the library and render the results they way I want, but when I attempt to create a compound CAML query, I keep getting errors. I have tried using the various resources here to manually build this query (and do to network restrictions I cannot use the U2U CAML builder tool), but I keep getting thrown errors. I started doing simple queries and it was working, but once I try to get to my third and forth "OR" statements (including a IsNull argument), I hit a wall. I think I am improperly nesting my statements and would love some help in identifying what I am doing wrong.

    <Where>
      <And>
        <Contains>
           <FieldRef Name='Author' />
           <Value Type='User'>" + userName + "</Value>
        </Contains>

      <And>
        <Eq>
          <FieldRef Name='ContentType' />
          <Value Type='Computed'>Document</Value>
        </Eq>
      </And>
      <Or>
        <Or>
          <Or>
            <Or>
              <Eq>
                <FieldRef Name='sensitivity' />
                <Value Type='Choice'>Low</Value>
              </Eq>
              <Eq>
                <FieldRef Name='sensitivity' />
                <Value Type='Choice'>Medium</Value>
              </Eq>
            </Or>
              <Eq>
                <FieldRef Name='sensitivity' />
                <Value Type='Choice'>High</Value>
              </Eq>
          </Or>
         </Or>
        <IsNull>
           <FieldRef Name='sensitivity' />
        </IsNull>
       </Or>
     </And>
    </Where>

The desired result would be an output of items that the current user uploaded / created WHERE the Content Type is equal to "Document" AND the Sensitivity (choice) field is equal to Low, Medium, High or is empty.

I feel like I am really close, but I am making a silly mistake. For context, the "userName" reference in the statement is a javascript variable that is functioning properly.

1

There are 1 best solutions below

0
Ryan McGuirk On

The bracketing can be quite confusing without a tool like the U2U CAML builder tool. I feel your pain.

I think your issue may be the isnull at the end. I believe you would want is NotNull

  <IsNotNull>  
    <FieldRef Name='sensitivity' />   
  </IsNotNull>  

As a last ditch you might be able to just query all files by user whos documents co