Search code examples
sharepointsharepoint-onlinesharepoint-rest-api

How to access all document libraries/folder/files in a sharepoint online site


Below api is not returning lists/document libraries/folders/files for any site(root/created by me) and subsites.

{{url}}/_api/web/lists

I need an api so that I can fetch all content of a site just like there are api's for MS Graph.

"#{MS_GRAPH_SITE}/sites/#{site_id}/lists"

And then items:

"#{MS_GRAPH_SITE}/sites/#{site_id}/lists/#{list['id']}/items"

I am able to fetch sites lists through below API:

{{url}}/_api/search/query?querytext='contentclass:STS_Site contentclass:STS_Web'

Solution

  • For every site, I fetched lists through below endpoint:

    /_api/web/lists?$select=id&$filter=hidden eq false and iscatalog eq false and basetemplate eq 101 and itemcount ne 0
    

    Then for every lists, I fetched folders and files:

    /_api/web/lists/getbyid('#{list_id}')/rootfolder?$expand=folders,files
    

    Let's call above list_folder_files.

    list_folders = list_folders_files['Folders'].select{|x| x['ItemCount'] != 0}

    And for every folder, I fetched subfolder and files:

    /_api/web/getfolderbyserverrelativeurl('#{list_folder['ServerRelativeUrl']}')?$expand=folders,files