0

How Retrieve all the files and folders recursively in a specific folder using SPFx REST API post method?

private _spApi(): void {
    this.context.spHttpClient
      .post(`${this.context.pageContext.web.absoluteUrl}/_api/web/Lists/GetByTitle('Title')/GetItems(query=@v1)?$expand=File&@v1={"ViewXml":"<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileDirRef' /><Value Type='Text'>/Folder/Subfolder</Value></Eq></Where></Query></View>"}`,
        SPHttpClient.configurations.v1,
        {
          headers: [
            ['accept', 'application/json;odata=nometadata'],
            ['odata-version', 'v4.0']
          ]
        })
      .then(res => res.json())
      .then(web => console.log("Web: ", web)).catch(error => console.log("Error: ",error));
  }
2
  • Are you getting any error while using above code? Commented Mar 26, 2023 at 16:48
  • I am checking to see how things are going there on this issue. Commented Apr 6, 2023 at 6:01

1 Answer 1

0

Instead of including the base folder in there where clause of the CAML query, you should set it using the FolderServerRelativeUrl property of the query object.

private _spApi(): void {
  const siteUrl = this.context.pageContext.web.serverRelativeUrl;
  const options: ISPHttpClientOptions = {
    body: JSON.stringify({
      "query": {
        "ViewXml": "<View Scope='RecursiveAll'></View>",
        "FolderServerRelativeUrl": `${siteUrl}/Lists/Title/Folder/Subfolder`
      }
    })
  };

  const url = `${siteUrl}/_api/Web/Lists/GetByTitle('Title')/GetItems?$select=*,FileDirRef`;
  this.context.spHttpClient
    .post(url, SPHttpClient.configurations.v1, options)
    .then(res => res.json())
    .then(web => console.log("Web: ", web)).catch(error => console.log("Error: ",error));
}

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.