0

I'm using CAML Query to pull items from a list.

I tried re-arranging the structure of my query, and still would not return anything. I try not to use CAMLQuery Builder (doing it manually).

<Query>
    <Where>
        <And>
            <Contains>
                <FieldRef Name="Field1"/><Value Type="Text">A</Value>
            </Contains>
            <And>
                <In>
                    <FieldRef Name="Field2"/><Values><Value Type="Text">B</Value></Values>
                </In>
                <Or>
                    <In>
                        <FieldRef Name="Field3"/><Values><Value Type="Text">C</Value></Values>
                    </In>
                    <In>
                        <FieldRef Name="Field4"/><Values><Value Type="Text">D</Value></Values>
                    </In>
                </Or>
            </And>
        </And>
    </Where>
</Query>

Note: I have seen questions that may seem as a duplicate of this, but have never seen a question go as deep as this level of branching. CAML Query seems to be particular in where you put your closing tags.

Did I nest my "And" and "Or" incorrectly? What could be wrong/missing in this query?

Marlo C
  • 587
  • 3
  • 14
  • 26
  • Your query is incorrect: each comparison operator (for example Contains) should contains two other nodes - 1) which contains field name and somevalue which contains value to compare. http://msdn.microsoft.com/en-us/library/office/ms196501%28v=office.15%29.aspx – Yevgeniy.Chernobrivets Jun 23 '14 at 18:41
  • OP, we're assuming you omitted the `` and `` elements for brevity - please update the question with the full CAML. – Stevangelista Jun 24 '14 at 13:24
  • @Stevangelista Yes, I put "A, B, C, D" in place of those elements. I did it for simplicity's sake since my problem seems to be coming from the structure of its parent nodes. – Marlo C Jun 24 '14 at 13:41
  • @Yevgeniy.Chernobrivets can't have only 1, just like can? For example: – Marlo C Jun 24 '14 at 13:44
  • I believe what Yevgeniy meant was that `` needs two child elements; one `` and one `` - not two of each. – Stevangelista Jun 24 '14 at 13:49
  • From my experience if CAML query is malformed then query returns all items from list. You said that your query does not return any items so i suspect that no items meet conditions. It is hard to say anything about this situation without any item example. – Yevgeniy.Chernobrivets Jun 24 '14 at 14:26

1 Answers1

0

While your CAML appears valid to me (without actual <FieldRef> and <Value> elements, I can't specifically test your field names, types & values), I tend to write my CAML in a more "balanced" fashion, as such:

<Query>
    <Where>
        <And>
            <And>
                <Contains>A</Contains>
                <In>B</In>
            </And>
            <Or>
                <In>C</In>
                <In>D</In>
            </Or>
        </And>
    </Where>
</Query>

According to the spec, both your posted version & my answer here would net the same result, but I would test to confirm. If the above doesn't work, brevity aside, I'd suggest posting your full CAML so that we can recreate your List with the same field names & types and test data with matching values.

Stevangelista
  • 1,799
  • 1
  • 10
  • 15
  • Edited. I just didn't want to reveal my field names and search values. – Marlo C Jun 24 '14 at 13:48
  • I can appreciate needing to sanitize the data, but if that's really what your CAML looks like, those `` conditions are not correct. The `` operator has a child `` element, which itself has `n` number of ` elements. Also, as the `` operator is designed to simplify having to write multiple `` conditions inside an `` branch, you shouldn't use it for only a single condition. – Stevangelista Jun 24 '14 at 14:07
  • thanks for catching that! I actually am using `` for my `` but forgot to put them here. It's still not working. – Marlo C Jun 24 '14 at 15:50
  • Then as Yevgeniy also commented, without real column names, data types, filter values and sample items, it'll be impossible to test further. My answer and your CAML are both valid to the CAML spec, but to get an answer on why it's not working in your specific use case, more detail will be needed. – Stevangelista Jun 24 '14 at 15:54