0

I want to query for SharePoint items that contain at least one or more specified texts. For example, I have a field called "Pets." A SharePoint item would have a value that looks something like "Cat, Snake, Spider" or "Bear, Rat." So the number of pets will vary. If I want to only query for items that contain the words "Cat," "Dog," or "Frog," how would I do that using CAML query? The number of words I'll be checking for will be dynamic.

I tried this:

<Query>
    <Where>
        <Contains>
            <FieldRef Name='Pets' />
            <Value Type='Text'>Cat</Value>
        </Contains>
        <Contains>
             <FieldRef Name='Pets' />
             <Value Type='Text'>Dog</Value>
        </Contains>
        <Contains>
            <FieldRef Name='Pets' />
            <Value Type='Text'>Frog</Value>
        </Contains>
    </Where>
</Query>
0

You can use Or construct like:

<Query>
    <Where>
        <Or>
        <Or>
        <Contains>
            <FieldRef Name='Pets' />
            <Value Type='Text'>Cat</Value>
        </Contains>
        <Contains>
             <FieldRef Name='Pets' />
             <Value Type='Text'>Dog</Value>
        </Contains>
        </Or>
        <Contains>
            <FieldRef Name='Pets' />
            <Value Type='Text'>Frog</Value>
        </Contains>
         </Or>
    </Where>
</Query>
2
  • Thanks for the reply! This would only work if I'm looking for 3 words though right? I'm looking for something that's dynamic. For example, I would be checking if the field contains at least one of the 3, 4, or even 10 words I'm looking for.
    – Paul Vu
    Oct 9 '20 at 13:55
  • Yes, it will return all items that contain these values for field [Pets]. If you would like to add more words, you just need to add more <Or> conditions. Let me know.. or post a sample that you would like to see. Oct 9 '20 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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