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 ()
{
}
}
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
{
}
}
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
{
}
return null;
}
public void UpdateRecords(DataSet dataSet)
{
try
{
SqlCommandBuilder cb = new SqlCommandBuilder(dataAdapter);
dataAdapter.UpdateCommand = cb.GetUpdateCommand(true);
dataAdapter.Update(dataSet);
}
catch
{
}
}
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.