0

I am creating an SPFX web part which queries a list in SharePoint Online

This works fine:

camlQuery = "<Query><OrderBy><FieldRef Name='LinkTitle' Ascending='TRUE' /></OrderBy></Query>"

It brings back all values from the list.

However if I try to just bring back certain rows:

camlQuery = "<View><Query><Where>";

        camlQuery += "<Or>";
        camlQuery += "<Eq><FieldRef Name='onPremisesSamAccountName'></FieldRef><Value Type='Text'>bloggsj</Value></Eq>";
        camlQuery += "<Eq><FieldRef Name='onPremisesSamAccountName'></FieldRef><Value Type='Text'>bloggsa</Value></Eq>";
        camlQuery += "<Eq><FieldRef Name='onPremisesSamAccountName'></FieldRef><Value Type='Text'>bloggsb</Value></Eq>";

        camlQuery += "</Or>";
        
        camlQuery += "</Where></Query></View>";

I get the error:

Error making HttpClient request in queryable [500] Internal Server Error ::> {"odata.error":{"code":"-2147467259, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"Cannot complete this action.\n\nPlease try again."}}}

1 Answer 1

1

Use CAML query in this format:

<View>
    <Query>
        <Where>
            <Or>
                <Eq><FieldRef Name='onPremisesSamAccountName'></FieldRef><Value Type='Text'>bloggsj</Value></Eq>
                <Or>
                    <Eq><FieldRef Name='onPremisesSamAccountName'></FieldRef><Value Type='Text'>bloggsa</Value></Eq>
                    <Eq><FieldRef Name='onPremisesSamAccountName'></FieldRef><Value Type='Text'>bloggsb</Value></Eq>
                </Or>
            </Or>
        </Where>
    </Query>
</View>

Note

The Or element can be nested inside other Or and And elements. The server supports unlimited complicated queries. However, any given Or element can have only two child elements. If you need to join three or more conditions, you must nest the Or elements.

Documentation: Or element (Query)

Update from comments:

You can write CAML query in below format for 6 users:

<View>
    <Query>
        <Where>
            <Or>
                <Eq><FieldRef Name='onPremisesSamAccountName'></FieldRef><Value Type='Text'>user1</Value></Eq>
                <Or>
                    <Eq><FieldRef Name='onPremisesSamAccountName'></FieldRef><Value Type='Text'>user2</Value></Eq>
                    <Or>    
                        <Eq><FieldRef Name='onPremisesSamAccountName'></FieldRef><Value Type='Text'>user3</Value></Eq>
                        <Or>    
                            <Eq><FieldRef Name='onPremisesSamAccountName'></FieldRef><Value Type='Text'>user4</Value></Eq>
                            <Or>    
                                <Eq><FieldRef Name='onPremisesSamAccountName'></FieldRef><Value Type='Text'>user5</Value></Eq>
                                <Eq><FieldRef Name='onPremisesSamAccountName'></FieldRef><Value Type='Text'>user6</Value></Eq>
                            </Or>
                        </Or>
                    </Or>
                </Or>
            </Or>
        </Where>
    </Query>
</View>
7
  • That worked many thanks Ganesh. If I have 10 users to check does it follow the same format? My problem is that the amount of users changes. Thanks so much. Commented Feb 27, 2023 at 11:51
  • 1
    Yes, you have to follow same format - Use only 2 child statements inside OR element with nesting. Commented Feb 27, 2023 at 11:52
  • Thanks. Would there be a better way to build a caml query to only show the rows where onPremisesSamAccountName = "user1;user2;user3;user4;user5;user6;" ? Commented Feb 27, 2023 at 11:55
  • Check updated answer for example of more complex query. Commented Feb 27, 2023 at 12:02
  • 1
    Sure, I have previously created a JavaScript function for dynamically generating such CAML queries. Let me find it and I will share it as an answer on your post. Commented Feb 27, 2023 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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