Sunday, March 27, 2011

Upload files to SharePoint 2007 from Network Share


To upload files to SharePoint document library from a network share we need to create a SharePoint timer job. I was written a SharePoint timer job which iterates through all the folders of the network share and uploads all the files present in the folders to a SharePoint document library. For your convenience of debugging I will be posting a console application code below. Once you feel everything is working as expected please convert it to a SharePoint timer Job.
namespace CopyDocumentsFromSharedFolderToSharePoint {
class Program
{
const string NetworkPath = "\\\\ServerName\\Reports";
const string SharePointPath = "http://SiteAddress/sites/Reports/Staging Reports/TEST/";
const double maxFileSize = 52428800;//50MB in Bytes 50*1024*1024
static void Main(string[] args)
{
ProcessFolder(NetworkPath);
Console.ReadKey();
}
private static void ProcessFolder(string startingPath)
{
int iterator = 0;
List<string> dirList = new List<string>();
dirList.Add(startingPath);
string parentFolder = startingPath;
// Every new folder found is added to the list to be searched. Continue until we have
// found, and reported on, every folder
while (iterator < dirList.Count)
{
parentFolder = dirList[iterator]; // Each FileTreeEntry wants to know who its parent is
try
{
foreach (string dir in Directory.GetDirectories(dirList[iterator]))
{
dirList.Add(dir);
}
foreach (string filename in Directory.GetFiles(dirList[iterator]))
{
FileInfo file = new FileInfo(filename);
int returnCode = validateFileUploadToSharePoint(file);
if (returnCode == 0)
{
UploadFileToSharePoint(filename, (SharePointPath + file.Name));
}
Console.WriteLine(file.Name);
}
}
// There are two *acceptable* exceptions that we may see, but should not consider fatal
catch (UnauthorizedAccessException ex1)
{
}
catch (PathTooLongException ex2)
{
}
catch (DirectoryNotFoundException ex3)
{
}
catch (Exception ex4)
{
}
iterator++;
}
}
private static int validateFileUploadToSharePoint(FileInfo file)
{
//ensure folder path exists
//Check if file size exceeds 50MB
if (file.Length > maxFileSize)
{
//file length is greater then 50MB
return 2;
}
return 0;
}
protected static void UploadFileToSharePoint(string UploadedFilePath,string SharePointPath)
{
WebResponse response = null;
try
{
// Create a PUT Web request to upload the file.
WebRequest request = WebRequest.Create(SharePointPath);
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "PUT";
// Allocate a 1 KB buffer to transfer the file contents.
// You can adjust the buffer size as needed, depending on
// the number and size of files being uploaded.
byte[] buffer = new byte[1024];
// Write the contents of the local file to the
// request stream.
using (Stream stream = request.GetRequestStream())
using (FileStream fsWorkbook = File.Open(UploadedFilePath,FileMode.Open, FileAccess.Read))
{
int i = fsWorkbook.Read(buffer, 0, buffer.Length);
while (i > 0)
{
stream.Write(buffer, 0, i);
i = fsWorkbook.Read(buffer, 0, buffer.Length);
}
}
// Make the PUT request.
response = request.GetResponse();
File.Delete(UploadedFilePath);
}
catch (Exception ex)
{
throw ex;
}
finally
{
response.Close();
}
}
}
}
References:
http://www.scottleckie.com/2009/07/iterating-through-a-bunch-of-folders-and-files/
http://blogs.msdn.com/b/erikaehrli/archive/2009/06/30/how-to-upload-files-to-sharepoint-server-2007-from-asp-net-web-applications.aspx
http://msdn.microsoft.com/en-us/library/dd902097.aspx

1 comment:

  1. Hi Bharat,

    I tried the above code in my console application, but when i am running the code i got the below error,
    " The remote server returned an error:(409)Conflit"
    Could you please tell me why this error occured.

    ReplyDelete