0

I'm trying to do a complex filter in a list view. I'm used to boolean searches and using parenthesis to make sure that the AND/OR are executed in the correct order. But I can't figure out how to enter the filters in the UI. The filter I want is

(Condition1 OR Condition2 OR Condition3 OR Condition4) AND (Condition5 OR Condition6)

I've opened the list view aspx page in SharePoint designer to see how the CAML query is and it looks somethine like this.

<Where>
  <Or>
    <And>
      <Or>
        <Or>
          <Or>
            <Eq>
              Condition1
            </Eq>
            <Eq>
              Condition2
            </Eq>
          </Or>
          <Eq>
            Condition3
          </Eq>
        </Or>
        <Eq>
          Condition4
        </Eq>
      </Or>
      <Eq>
        Condition5
      </Eq>
    </And>
    <Eq>
      Condition6
    </Eq>
  </Or>
</Where>

Which works out to the following (I think).

(Condition1 OR Condition2 OR Condition3 OR Condition4) AND Condition5 OR Condition6

I've tried moving the and , but I just can't wrap my head around how to get what I want.

I've search and found several recommendations for tools like SP CAML Query Helper, but it only helps write out the statements...you still need to know what order to put them in.

Can anybody help me make sense of this and how I can structure what I want either by editing the CAML query or using the web UI?

Additional tests

I also found this thread. Based on that suggestion I tried the following, which seemed to work when I put it into the query using SharePoint Designer.

<Where>
  <And>
    <Or>
      <Or>
        <Or>
          <Eq>
            Condition1
          </Eq>
          <Eq>
            Condition2
          </Eq>
        </Or>
        <Eq>
          Condition3
        </Eq>
      </Or>
      <Eq>
        Condition4
      </Eq>
    </Or>
    <Or>
      <Eq>
        Condition5
      </Eq>
      <Eq>
        Condition6
      </Eq>
    </Or>
  </And>
</Where>

Unfortunately when I added a new column to the view using the web interface, the query was mangled and stopped working.

So additionally, I need a solution that will either work through SPD or web interface, but won't get mangled if the web interface is used again.

1

If you are limiting it to the dropdown filters in the view edit form, you're more limited than a standard CAML Query.

The view filters only work '2 at a time', best way I can describe it. You can't mix ANDs and ORs.

Result from Filter 1 and/or Filter 2
The above And/OR Filter 3
The above And/Or Filter 4

What I usually try to do is all the generic OR statements first to get those out of the way. Then do the ANDs. And if you really do need to mix AND/OR, you'll need to use calculated fields to simplify your logic. Then you can test using the calculated field.

What you've got in your example is:

Condition1 OR Condition2 OR Condition3 OR Condition4
That answer, AND Condition5
That answer OR Condition6

So Condition6 will always be tested on its own. Condition1-4 will have to test true and Condition5 will also have to be true.

  • Bummer that there is no direct way to do this. Ended up going with the calculated field. Thanks for that suggestion. – Rothrock Jan 17 '18 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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