Click here to Skip to main content
15,890,438 members
Articles / Web Development / ASP.NET
Tip/Trick

Downloading Files from SharePoint Server to Network Drive

Rate me:
Please Sign up or sign in to vote.
1.44/5 (4 votes)
8 Feb 2016CPOL1 min read 10.9K   2   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

C++
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:

 

Image 1

C++
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();

    }
}
C++
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();
}
C++
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
     }
}
C++
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;
}
C++
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNot an article Pin
Nelek19-Jan-16 5:48
protectorNelek19-Jan-16 5:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.