0

When I run the following query I get an error:

<View><Query><Where>
<Or><Eq><FieldRef Name='ID'/><Value Type='Number'>164</Value></Eq>
<Or><Eq><FieldRef Name='ID'/><Value Type='Number'>166</Value></Eq>
<Or><Eq><FieldRef Name='ID'/><Value Type='Number'>154</Value></Eq>
<Or><Eq><FieldRef Name='ID'/><Value Type='Number'>165</Value></Eq>
</Or></Or></Or></Or></Where></Query><RowLimit>50</RowLimit></View>

I've also tried:

<View><Query><Where><Or>" 
<Eq><FieldRef Name='ID'/><Value Type='Number'>164</Value></Eq>
<Eq><FieldRef Name='ID'/><Value Type='Number'>166</Value></Eq> 
<Eq><FieldRef Name='ID'/><Value Type='Number'>154</Value></Eq> 
<Eq><FieldRef Name='ID'/><Value Type='Number'>165</Value></Eq> 
</Or></Where></Query><RowLimit>50</RowLimit></View>

It also fails. and before that I tried using In:

<Query><Where><In><FieldRef Name='ID'/><Values> <Value
Type='Number'>166</Value><Value
Type='Number'>165</Value></Values></In></Where></Query>

And that returns all the items. Not the ones "In" my provided query.

What I want is to be able to build my caml query from the selected items. I've managed to get Ids of the selected items and now I want to load my list object with the list properties that I need (clientContext.load(listItems, 'Include(Id, Title,...,...)') ).

I'm stuck with the caml query that I trying to build dynamicly. Can anyone spot what I'm doing wrong?

2 Answers 2

5

You can try below query(removed one extra Or operator from your code, since to join 4 statement you need only 3 operator. ):

<View><Query><Where>
<Or><Eq><FieldRef Name='ID'/><Value Type='Number'>164</Value></Eq>
<Or><Eq><FieldRef Name='ID'/><Value Type='Number'>166</Value></Eq>
<Or><Eq><FieldRef Name='ID'/><Value Type='Number'>154</Value></Eq>
<Eq><FieldRef Name='ID'/><Value Type='Number'>165</Value></Eq>
</Or></Or></Or></Where></Query><RowLimit>50</RowLimit></View>

But since this has a lot of Or conditions it is always preferable to use In operator, instead of nested Or. Below is a sample In operation which is working for me:

<View><Query><Where><In><FieldRef Name='ID' /><Values>
                                        <Value Type='Integer'>164</Value>
                                        <Value Type='Integer'>166</Value>
                                        <Value Type='Integer'>154</Value>
                                        <Value Type='Integer'>165</Value>
                </Values></In></Where></Query></View>
2

You can only have 2 nodes in an And or Or block, so your CAML is malformed. If you are trying to build this up dynamically, then your best bet will be to use the In like you are and for each selected item, create a value node like you are currently doing. This is going to be much more simplistic than trying to properly build out the proper Or structure.

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.