$CAMLQuery = "<View Scope='RecursiveAll'>  
        <Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>bad_file</Value></Eq></Where> 
    <ViewFields><FieldRef Name='FileRef' /><FieldRef Name='FileLeafRef' /></ViewFields> 

I have the following CAML query, but it only selects files whose name is exactly "bad_file", but doesn't select files whose name is "bad_file.json" or "bad_file.txt". Is there a way to use regex so that I can select all files with the substring "bad_file"?

1 Answer 1


There is no support for regex within CAML, but there is a CONTAINS element that if you used like

<Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>bad_file</Value></Contains>

it would have matched both "bad_file.json" and "bad_file.txt".

Also, this is a common mistake, the CAML schema is slightly different depending on where you are using it. The <View> element is only the parent element when you are defining the schema for a custom View. If you are using it to execute a query programmatically through PowerShell,CSOM,JavaScript, etc., then <Query> should be your top-level, and the <View> is an optional child element.

Try this:

<Query><Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>bad_file</Value></Contains><View Scope='RecursiveAll'><ViewFields><FieldRef Name='FileRef' /><FieldRef Name='FileLeafRef' /></ViewFields></View></Query>
  • $CAMLQuery = "<View Scope='RecursiveAll'> <Query> <Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>bad_file</Value></Contains> </Query> <ViewFields><FieldRef Name='FileRef' /><FieldRef Name='FileLeafRef' /></ViewFields> </View>" selects every file for some reason.
    – Sayaman
    Mar 6, 2020 at 16:02
  • I just noticed that you fell victim to the classic blunder :) of the <View> vs. <Query> schema. I updated my answer for you.
    – willman
    Mar 6, 2020 at 16:09
  • Hmm, it matched the folder and the json file next to the folder effectively deleting everything even though the folder name and the file name were asset-content and content.json
    – Sayaman
    Mar 6, 2020 at 16:12
  • are you actually searching for "bad_file" or was that just an example?
    – willman
    Mar 6, 2020 at 17:01
  • I am searching for anything with bad_file as a substring like "bad_file.json", "bad_file.txt", etc.
    – Sayaman
    Mar 6, 2020 at 17:08

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.