Download all files from Sharepoint Online Document Library using C# CSOM

In this article, we are going to see how to download the files and folders to the local system along with the folder structure using C# CSOM

  1. Create a console application with .net framework
  2. Add SharePoint online CSOM ddl’s using NuGet packages( Microsoft.SharePointOnline.CSOM).
  3. For this example, I am using this Nuget package. 

Code :

        public static void DownloadAllDocumentsfromLibrary()
        {
            ClientContext ctxSite = GetSPOContext();
            string libraryname = "DownloadCSOM";
            var list = ctxSite.Web.Lists.GetByTitle(libraryname);
            var rootFolder = list.RootFolder;
            string pathString = string.Format(@"{0}{1}\", @"C:\", libraryname);
            if (!Directory.Exists(pathString))
                System.IO.Directory.CreateDirectory(pathString);
            GetFoldersAndFiles(rootFolder, ctxSite, pathString);
        }

        private static void GetFoldersAndFiles(Folder mainFolder, ClientContext clientContext, string pathString)
        {
            try
            {
                clientContext.Load(mainFolder, k => k.Name, k => k.Files, k => k.Folders);
                clientContext.ExecuteQuery();
                foreach (var folder in mainFolder.Folders)
                {
                    string subfolderPath = string.Format(@"{0}{1}\", pathString, folder.Name);
                    if (!Directory.Exists(subfolderPath))
                        System.IO.Directory.CreateDirectory(subfolderPath);

                    GetFoldersAndFiles(folder, clientContext, subfolderPath);
                }

                foreach (var file in mainFolder.Files)
                {
                    var fileName = Path.Combine(pathString, file.Name);
                    if (!System.IO.File.Exists(fileName))
                    {
                        var fileRef = file.ServerRelativeUrl;
                        var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
                        using (var fileStream = System.IO.File.Create(fileName))
                        {
                            fileInfo.Stream.CopyTo(fileStream);
                        }
                    }
                }

            }
            catch (Exception ex)
            {

            }
        }

        private static ClientContext GetSPOContext()
        {

            string UserName = "user@domain.com";
            string spsiteurl = "https://tenant.sharepoint.com/sites/sitename/";
            string Pwd = "Password";
            var secure = new SecureString();
            foreach (char c in Pwd)
            {
                secure.AppendChar(c);
            }
            ClientContext spoContext = new ClientContext(spsiteurl);
            spoContext.Credentials = new SharePointOnlineCredentials(UserName, secure);
            return spoContext;

        }

Points to remember:

  • The above will download  all the files with the same structure as in the document library
  • Please use proper  and valid credentials  and site Url while creating the context
  • The code will also download the default forms folder in the document library
  • please update the code as per your requirement

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s