Download a folder with multiple sub folders as a single zip file from SharePoint

Sudharsan Varadharajan 96 Reputation points
2021-06-23T11:57:52.643+00:00

Hi,

I am looking to download a folder from SharePoint programmatically which has 300+ files. I can download each file individually, but it takes a really long time to download the entire folder.

I would like to know if there is a way to download the entire folder as a zip using Python? (this seems to be the normal behavior when we download folder from SharePoint directly) Reference code in any prog. language is also fine.

Thanks

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,607 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sudharsan Varadharajan 96 Reputation points
    2021-06-29T09:57:34.247+00:00

    Hi @MichaelHan-MSFT

    Thanks for the follow up. I appreciate it :)

    Regarding the Python reference code, yes, the ref code just downloads the files in the first level. But I have a recursive implementation.

    On the overall issue, I still don't have a clean solution. It would be great if Microsoft can expose an API to download folder as a zip. But, for now, we are just planning to solve it by zipping the folders and then upload to SP. The downside here is, even if need a single file, I might end up downloading the entire zip.

    I am planning to request my users to zip the folders and upload them in SharePoint, and place the frequently accessed files separately (for now, it is just one file that I have to access often). And when in need, through the SP Automation code, download the entire zip and unzip it programmatically after download. We decided to temporarily go ahead with this workflow.

    Thanks!


1 additional answer

Sort by: Most helpful
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-06-24T02:59:57.117+00:00

    Hi @Sudharsan Varadharajan ,

    In SharePoint there is no direct way to dowanload the entire folad as a zip file. We have to download the folder using CSOM then zip the folder.

    I'm not familiar with python, below is my demo code for you to download the entire folder written in C#:

    private static void GetFoldersAndFiles(Folder mainFolder, ClientContext clientContext, string pathString)  
            {  
                clientContext.Load(mainFolder, k => k.Files, k => k.Folders);  
                clientContext.ExecuteQuery();  
                foreach (var folder in mainFolder.Folders)  
                {  
                    string folderPath = string.Format(@"{0}\{1}", pathString, folder.Name);  
                    System.IO.Directory.CreateDirectory(folderPath);  
      
                    GetFoldersAndFiles(folder, clientContext, folderPath);  
                }  
      
                foreach (var file in mainFolder.Files)  
                {  
                    var fileRef = file.ServerRelativeUrl;  
                    var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);  
                    var fileName = Path.Combine(pathString, file.Name);  
                    using (var fileStream = System.IO.File.Create(fileName))  
                    {  
      
                        fileInfo.Stream.CopyTo(fileStream);  
                    }  
                }  
            }  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.