0

The And block of Field3 is being ignored. It doesn't matter if the text matches or not. What's wrong here?

<Where>
  <And>
    <Eq>
      <FieldRef Name='Field1' />
      <Value Type='Boolean'>1</Value>
    </Eq>
    <Eq>
      <FieldRef Name='Field2' />
      <Value Type='Text'>Something</Value>
    </Eq>
  </And>
  <And>
    <Contains>
      <FieldRef Name='Field3' />
      <Value Type='Text'>Testing</Value>
    </Contains>
  </And>
</Where>
<OrderBy>
  <FieldRef Name='Field8' Ascending='True' />
</OrderBy>

Thanks!

4
<Query>
    <Where>
        <And>
            <Eq>
                <FieldRef Name="ID" /><Value Type="Counter">1</Value>
            </Eq>
            <And>
                <Eq>
                    <FieldRef Name="ContentType" /><Value Type="Computed">test</Value>
                </Eq>
                <Eq>
                    <FieldRef Name="Title" /><Value Type="Text">aaa</Value>
                </Eq>
            </And>
        </And>
    </Where>
</Query>

It should be like this. Use U2U caml builder.

3

I've had it work like this, nesting the Ands at the beginning

<Where>
  <And><And>
    <Eq>
      <FieldRef Name='Field1' />
      <Value Type='Boolean'>1</Value>
    </Eq>
    <Eq>
      <FieldRef Name='Field2' />
      <Value Type='Text'>Something</Value>
    </Eq>
  </And>
    <Contains>
      <FieldRef Name='Field3' />
      <Value Type='Text'>Testing</Value>
    </Contains>
  </And>
</Where>
<OrderBy>
  <FieldRef Name='Field8' Ascending='True' />
</OrderBy>
  • 1
    Combining operators in CAML is something of a black art and something I find impossible to describe, even after writing (horrible) code that generates CAML I still can't describe effectively how the nesting works, but yes, it involves nesting all but the last clause for every second degree. – James Love Dec 27 '11 at 19:49
  • Black art is an understatement. Yes, It took me awhile to realize that they go at the beginning of the query. – David King Dec 27 '11 at 20:20
  • If I was ever to make a query like this I would test and log with linq first, the dangers of making a query like this extremely slow are high. This query should be okay, it all depends on the amount of data the contains is sifting through, 1 contains is okay, 2 is iffy and 3+ you really need to balance. – Hugh Wood Aug 28 '12 at 9:09
1

You can also use Camlex.NET. It takes care of the nesting when you use the Camlex.Query().WhereAll(expressions).ToString() method.

Camlex.NET on Codeplex

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.