Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!

share|improve this question
add comment

3 Answers 3

up vote 4 down vote accepted
<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.

share|improve this answer
    
Thanks, I'd never used nested And's before. –  David King Dec 27 '11 at 20:20
add comment

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>
share|improve this answer
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
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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