Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Downloading Files from SharePoint Server to Network Drive

0.00/5 (No votes)
8 Feb 2016 1  
Downloading Files from SharePoint Server to Network Drive

Introduction

Organizations today use SharePoint Server to create SharePoint portals which are used as a secure place for organizing, sharing and accessing data. It is capable of providing browser based management and administrative tools that enable users to create or edit documents independently. The share point files are stored in database as bytes. It has been seen in instances that a huge bunch or archived files create a memory crunch in database causing the user to take increased database storage which in turn cause them increased expense.

In order to resolve this issue we can download the files to a network drive and can replace the file in SharePoint with a Link-to-Document. In this article we will focus only on the downloading service, to download list of documents from SharePoint to Network drive.

Using the code

First Approach: Without using multithreading

private static void DownloadAttachment(string url, string fileName, string folderName, string filePath)
{
    try
    {
        if (Directory.Exists(filePath))
        {
            if (!Directory.Exists(filePath + folderName))
            {
                Directory.CreateDirectory(filePath + folderName);
            }
            WebClient Client = new WebClient();
            Client.Credentials = System.Net.CredentialCache.DefaultCredentials;
            Client.DownloadFile(url, filePath + folderName + @"\" + fileName);
        }
     }
     catch ()
     {
         //Log error
     }
}

Second Approach: Using multithreading

Multithreading is basically used to execute more than one task at a time within a process. Creating and destroying threads is a resource extensive process. So in order to harness the advantage of parallel processing and get maximum performance we use threads.

In order to use multi threading, we can create a table in database, which can be populated with the list of records containing file information to be downloaded from the SharePoint site. The table structure can be as below:

 

public void Download()
{
    string path, fileName, folderName, downloadUrl, sharePointPath;
    DataSet dataSet = GetRecordsToDownload();
    if (dataSet != null && dataSet.Tables[0] != null && dataSet.Tables[0].Rows.Count > 0)
    {
        Task parent = new Task(() =>
        {
             foreach (DataRow row in dataSet.Tables[0].Rows)
             {
                  downloadUrl = string.Format(@"{0}{1}/{2}.{3}",  
                                    row["Server_Url"].ToString(), 
                                    row["File_Name"].ToString(), 
                                    row["File_Extension"].ToString());

                  fileName = string.Format("{0}.{1}", row["File_Name"].ToString(), 
                                    row["File_Extension"].ToString());

                  folderName = row["Folder_Name"].ToString();
                  path = string.Format(@"{0}\", row["Storage_Path"].ToString());
                  DownloadAsync(downloadUrl, fileName, folderName, path, row);
              }
         });

         parent.Start();
         parent.Wait();

         UpdateRecords();

    }
}
private static void DownloadAsync(string url, string fileName, string folderName, 
                                   string path, DataRow dataRow)
{
     new Task(() => DownloadAttachment(url, fileName, folderName, path, dataRow), 
                                     TaskCreationOptions.AttachedToParent).Start();
}
private static void DownloadAttachment(string url, string fileName, string folderName, string filePath
                                         DataRow dataRow)
{
    try
    {
        if (Directory.Exists(filePath))
        {
            if (!Directory.Exists(filePath + folderName))
            {
                Directory.CreateDirectory(filePath + folderName);
            }
            WebClient Client = new WebClient();
            Client.Credentials = System.Net.CredentialCache.DefaultCredentials;
            Client.DownloadFile(url, filePath + folderName + @"\" + fileName);
            dataRow["Status"] = 2;
        }
     }
     catch
     {
         //Log error
     }
}
public static SqlDataAdapter dataAdapter = new SqlDataAdapter();

private static DataSet GetRecordsToDownload()
{
    try 
    {
         string connectionString = "server=local;database=Test;IntegratedSecurity=SSPI;";
         string query = "SELECT TOP 1000 * FROM tblDocuments WHERE ArchivalStatus=1";
         SqlConnection connection = new SqlConnection(connectionString);
         SqlCommand command = new SqlCommand(query, connection);
         dataAdapter.SelectCommand = command;
         DataSet dataSet = new DataSet();
         dataAdapter.Fill(dataSet);
         return dataSet;
    }
    catch
    {
         //Log error
    }
    return null;
}
public void UpdateRecords(DataSet dataSet)
{
    try
    {
        SqlCommandBuilder cb = new SqlCommandBuilder(dataAdapter);
        dataAdapter.UpdateCommand = cb.GetUpdateCommand(true);
        dataAdapter.Update(dataSet);
    }
    catch
    {
        //Log error
    }
}

Points of Interest

Multithreading improved the processing time drastically. While implementing Downloading functionality without mutithreading, it took around 1 hour to download 4 GB data from SharePoint portal to Network drive. The same set of data was downloaded in just 2 minutes with the help of mutithreading.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here