2

I need to download all files from sharepoint in a group of subfolders, but I'm struggling to get the list of files to use the download Method.

I'm using the python library Office365-REST-Python-Client, specifically the enum_files_and_folders.py example.

The problem is my document library is too large. Then a want to enum only files in a specific subfolder.

My sharepoint structure is something like this:

https://mysite/shared documents/General/.../sheets/year/month

I want to download all xlsx files in sheets folder and subfolders, without transversing all document library structure.

Does anyone know a filter option ?

My current code is something like this:

doc_lib = contexto.web.get_folder_by_server_relative_url(relative_url)
items = doc_lib.items.select(["FileSystemObjectType"]).expand(["File", "Folder"]).get().execute_query()
arquivos_fluxo =[]
for item in items:  # type: ListItem
    if item.file_system_object_type != FileSystemObjectType.Folder:
        if "myfiles.xlsx" in item.file.serverRelativeUrl:
            print("File url: {0}".format(item.file.serverRelativeUrl))
            arquivos_fluxo.append(item.file.serverRelativeUrl)

1 Answer 1

3

i encountered a similar problem. Here's the code that I use to retrieve all files from a subfolder:

def get_all_files_from_document_library(spo_site: str, folder_name: str) -> list:
    try:
        user_credentials = UserCredential(global_settings.SYSTEM_USER_SPO, secret.SYSTEM_USER_PWD)
        ctx = ClientContext(f"{global_settings.SHAREPOINT_BASE_URL}/sites/{spo_site}").with_credentials(user_credentials)
        files = ctx.web.get_folder_by_server_relative_url(folder_name).files
        ctx.load(files).execute_query()
        file_list = []
        for file in files:
            file_list.append({"Name": file.name, "ServerRelativeUrl": file.serverRelativeUrl})

    except:
        logging.error("Could not retrieve files from folder '%s/%s'", spo_site, folder_name)
        return None
    return file_list

I used the answers posted on this github issue as a reference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.