SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

CAML Query <And> <Or> can only compare a pair at a time

It's strange that CAML query can only compare a pair of expressions either by <And> or <Or>. So if you have a query like the one follows, you will see an error message "Cannot complete this action. Please try again", which is not very helpful.

<Where>
  <And>
    <Gt>
        <FieldRef Name='Created' /><Value Type='DateTime'><Today OffsetDays='-30' /></Value>
    </Gt>
    <Neq>
        <FieldRef Name='ContentType' /><Value Type='Text'>Folder</Value>
    </Neq>
    <Neq>
        <FieldRef Name='ContentType' /><Value Type='Text'>InfoPath Form Template</Value>
    </Neq>
  </And>
</Where>

The workaround is to use nested <And> or <Or>, in which each operator only takes two expressions. The following snippet corrects the above one. This will make things more complicate especially for a number of conditions  mixed with <And> and <Or>.

<Where>
 <And>
  <And>
    <Gt>
        <FieldRef Name='Created' /><Value Type='DateTime'><Today OffsetDays='-30' /></Value>
    </Gt>
    <Neq>
        <FieldRef Name='ContentType' /><Value Type='Text'>Folder</Value>
    </Neq>
   </And>
    <Neq>
        <FieldRef Name='ContentType' /><Value Type='Text'>InfoPath Form Template</Value>
    </Neq>
  </And>
</Where>


Posted 11-27-2007 10:31 AM by jasonwj

Comments

Il blog di Luca Mauri wrote Sharepoint CAML Query with multiple
on 10-21-2008 8:30 AM

Sharepoint CAML Query with multiple

... wrote re: CAML Query <And> <Or> can only compare a pair at a time
on 03-05-2009 4:06 AM

Gute Arbeit hier! Gute Inhalte.

Matt Stark wrote re: CAML Query <And> <Or> can only compare a pair at a time
on 06-30-2009 11:07 AM

I think this is insane ... but you're right ... why would any moron do this to us ... grrrrrrrrrrrrrr

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.