0

Instead of below query, is possible to write dynamic CAML query for below CAML query. because below query will not work for dynamic data:

<Where>
    <Or>
        <Or>
            <Eq>
                <FieldRef Name='VehicleLine' />
                <Value Type='Text'>Test1</Value>
            </Eq>
        </Or>       
    <Or>
            <Eq>
                <FieldRef Name='VehicleLine' />
                <Value Type='Text'>Test2</Value>
            </Eq>
        </Or>       
    <Or>
            <Eq>
                <FieldRef Name='VehicleLine' />
                <Value Type='Text'>Test3</Value>
            </Eq>
        </Or>       
    </Or>
</Where>

I cannot find a close example for what I am trying to achieve, which is to check multiple fields for multiple values. Yes, I have looked on SO already for my answer, including this one: Need help on building dynamic CAML Query.

With that said, if it is possible, how can the following SQL-like query be written in CAML?

DECLARE @VehicleLines VARCHAR(50)='Test1','Test2','Test3'; --it can anything (based on user selection)

SELECT * FROM Master WHERE VehicleLine IN (@VehicleLines)

I want to use CAML query in javascript to fetch data from list.

1 Answer 1

0

Because it's a CSV, equals will never work. Consider using <Contains>:

<Where>
  <Or>
    <Or>
      <Contains>
        <FieldRef Name='VehicleLine' />
        <Value Type='Text'>Test1</Value>
      </Contains>
      <Contains>
        <FieldRef Name='VehicleLine' />
        <Value Type='Text'>Test2</Value>
      </Contains>
    </Or>
    <Contains>
      <FieldRef Name='VehicleLine' />
      <Value Type='Text'>Test3</Value>
    </Contains>
  </Or>
</Where>

Regarding dynamic building of Caml, there are many libraries that will do that for you. I've always used roboCAML for such requirements.

Your Answer

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

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