gymcode
gymcode

Reputation: 4623

CAML Query error when more than 2 variables in OR

In my SharePoint CAML query, when filtering with two input, it is successful. However, when I have 3 or more, it failed.

Is there a different format to be used when using more than two inputs?


Pass (2 fields):

<Where>
    <And>
        <Or>
            <Eq><FieldRef Name="Header1Ref"/><Value Type="Text">H1</Value></Eq>
            <Eq><FieldRef Name="Header1Ref"/><Value Type="Text">H2</Value></Eq>
        </Or>
            <Neq><FieldRef Name ="ContentType"/><Value Type="Text">Document</Value></Neq>
    </And>
</Where>

Fail (3 fields):

<Where>
    <And>
        <Or>
            <Eq><FieldRef Name="Header1Ref"/><Value Type="Text">H1</Value></Eq>
            <Eq><FieldRef Name="Header1Ref"/><Value Type="Text">H2</Value></Eq>
            <Eq><FieldRef Name="Header1Ref"/><Value Type="Text">H3</Value></Eq>
        </Or>
            <Neq><FieldRef Name ="ContentType"/><Value Type="Text">Document</Value></Neq>
    </And>
</Where>

Error:

Cannot complete this action. Please try again. at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx) at Microsoft.SharePoint.Library.SPRequest.GetListItemDataWithCallback2(IListItemSqlClient pSqlClient, String bstrUrl, String bstrListName, String bstrViewName, String bstrViewXml, SAFEARRAYFLAGS fSafeArrayFlags, ISP2DSafeArrayWriter pSACallback, ISPDataCallback pPagingCallback, ISPDataCallback pPagingPrevCallback, ISPDataCallback pFilterLinkCallback, ISPDataCallback pSchemaCallback, ISPDataCallback pRowCountCallback, Boolean& pbMaximalView) at Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData() at Microsoft.SharePoint.SPListItemCollection.get_Count()

Upvotes: 0

Views: 122

Answers (1)

Ondrej Tucny
Ondrej Tucny

Reputation: 27964

In the CAML syntax, both <Or> and <And> and binary operators. Hence they must have two operands only. If you need more, you have to nest them accordingly. In your case:

<Where>
    <And>
        <Or>
            <Or>
                <Eq><FieldRef Name="Header1Ref"/><Value Type="Text">H1</Value></Eq>
                <Eq><FieldRef Name="Header1Ref"/><Value Type="Text">H2</Value></Eq>
            </Or>
            <Eq><FieldRef Name="Header1Ref"/><Value Type="Text">H3</Value></Eq>
        </Or>
        <Neq><FieldRef Name ="ContentType"/><Value Type="Text">Document</Value></Neq>
    </And>
</Where>

Upvotes: 1

Related Questions