Search code examples
sharepointcamlsharepointdocumentlibrary

Search for folders and files by keyword in library with CAML


I am trying to search for files/folders whose names start with the entered text say 'foo'. I am able to retrieve files but the subfolders are not returned. Can anyone help. This is the CAML I am currently using:

<View Scope='Recursive'>
  <Query>
    <Where>
      <BeginsWith>
        <FieldRef Name='FileLeafRef' />
        <Value Type='Text'>foo</Value>
      </BeginsWith>
    </Where>
  </Query>
</View>

Solution

  • Try changing the scope to scope="RecursiveAll", this gets all files and all folders under the specified location. Also you should change:

    <Value Type='Text'>foo</Value>
    

    To

    <Value Type='File'>foo</Value>
    

    Also worth knowing is that FileLeafRef is the FILENAME (.docx etc) not the Title in SharePoint. Maybe you knew that :)