The secret code: moving your server files to cloud

Downloading Folders and Files (recursively) from SharePoint Library:

At times, we need to download files from a SharePoint library or a particular folder including sub-folders recursively to our local drive or a folder on our web application server (cloud). I am presenting the secret code to do so. In this article, we won’t need to use CAML queries or any complex piece of coding. We will just need to create two simple functions and that’s all. Happy Coding!

Using the code

Before using this code you’ll need the following libraries to be referenced in your project:
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.IO;
using System.Configuration;
using Microsoft.SharePoint.Client;
using System.Net;
Now we will write the code to perform the task. The first method we are going to set up is access to the SharePoint Library.  Once this is done we’ll call the other download and save all the files from SharePoint into a local folder.  See below the code you need to get started:
/// <summary>
/// Main Method to download the files in specified library from Share point
/// </summary>
private void DownloadSPFiles()
{
Boolean success = false;
try
{
string siteURL = “https://www.somesite.com”; //url of your sharepoint server
string uid = “User”; //Username to log on
string pwd = “Pwd”; //Password
string domain = “Domain”; //Domain

//We will initialize a context to communicate with Library
using (ClientContext context = new ClientContext(siteURL))
{
//Supply Credentials
context.Credentials = new NetworkCredential(uid, pwd, domain);

//set a folder in our web portal to save downloaded files from Sharepoint
string FolderPath = @”PathToSaveFIles”;

//First we need to get the folder by Relative URL of Sharepoint Library
var folder = ontext.Web.GetFolderByServerRelativeUrl(“library/books/CS”);
//library/books/CS is the Location of Library
context.Load(folder);
context.ExecuteQuery();

//Now call the function to recursively download and save files
DownloadFilesFolders(folder, context, FolderPath);
}
}
catch (Exception ex)
{
//your exception handling code
}
}

Here the second method which we are using.

/// <summary>
/// Method to get all files, folders recursively from sharepoint and save
/// </summary>
/// <param name=”ParentFolder”></param>
/// <param name=”clientContext”></param>
/// <param name=”pathString”></param>
private static void DownloadFilesFolders(Folder ParentFolder, ClientContext clientContext, string path)
{
try
{
clientContext.Load(ParentFolder, k => k.Files, k => k.Folders);
clientContext.ExecuteQuery();

//if folder has subfolder then loop through all and process them
foreach (var folder in ParentFolder.Folders)
{
string folderPath = string.Format(@”{0}{1}\”, path, folder.Name);

//For every folder found in SharePoint create one
System.IO.Directory.CreateDirectory(folderPath);

GetFoldersAndFiles(folder, clientContext, folderPath);
}

//For every file found in each folder, let’s download it
foreach (var file in ParentFolder.Files)
{
var fileReference = file.ServerRelativeUrl;
var fileInfo = File.OpenBinaryDirect(clientContext, fileReference);

//Set the location of this file so it’s saved in the folder we want.
var fileName = Path.Combine(pathString, file.Name);
//using File System to Save files
using (var fileStream = System.IO.File.Create(fileName))
{

//copy the files’ data
fileInfo.Stream.CopyTo(fileStream);
}
}
}
catch (Exception ex)
{
//your exception handling code
}
}
Enjoy coping our code to make your lives easier! If you want to learn more about our application development then contact Soluzione today!