SharePoint Products TechCenter >
SharePoint Products and Technologies Forums
>
SharePoint - Development and Programming
>
Upload folder from the local system to a document library programmatically ?
Upload folder from the local system to a document library programmatically ?
- How to upload a folder from my local system to SharePoint document library using the object model ?
Samarendra Swain Team Sharepoint www.manuhsolutions.com
Answers
Doing so by Standard-SP-API would be starting with a FileDirectory and reading all Files and all SubFolders, adding them to SP goes like that:
SPFolder docLib =web.Folders["MyDocLibName"];<br/>//Create File Stream fileContent = new FileStream("c:\MyDocument.doc", FileMode.Open, FileAccess.Read); <br/>SPFile newFile= docLib.Files.Add("MyDopcument.doc",fileContent); newFile.Update();<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>//Create Folders SPFolder newFolder = docLib.SubFolder.Add("NewFolder");<br/>newFolder.Update();<br/><br/>
You could this even do by an RPC-Put (so your Code must not be executed on SP-Server!)
Usage: UploadFileToDocumentLibrary(@”C:\test.txt”, @”http://machine/Shared Documents/textfile.txt”);public static bool UploadFileToDocumentLibrary(string sourceFilePath, string targetDocumentLibraryPath) { //Flag to indicate whether file was uploaded successfuly or not bool isUploaded = true; try { // Create a PUT Web request to upload the file. WebRequest request = WebRequest.Create(targetDocumentLibraryPath); //Set credentials of the current security context request.Credentials = CredentialCache.DefaultCredentials; request.Method = “PUT”; // Create buffer to transfer file byte[] fileBuffer = new byte[8192]; using (Stream stream = request.GetRequestStream()) { using (FileStream fsWorkbook = File.Open(sourceFilePath, FileMode.Open, FileAccess.Read)) { //Get the start point int startBuffer = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length); for (int i = startBuffer; i > 0; i = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length)) stream.Write(fileBuffer, 0, i); } } // Perform the PUT request WebResponse response = request.GetResponse(); //Close response response.Close(); } catch (Exception ex) { isUploaded = false; } return isUploaded; }- Proposed As Answer byMarkus I_ Wednesday, November 04, 2009 10:58 AM
- Marked As Answer byCharlie WuModeratorFriday, November 06, 2009 12:55 AM
- You can use the code available at codeplex for the import/export document utility called "SPIEFolder" http://www.codeplex.com/SPIEFolder. Its a simple console app so you might want to run it first to see the results , then open the code in visual studio. All the code is quite basic so I guess you can read it and get the understanding.Note on uplaoding huge files (e.g > 500MB)You may also want to look into other ways to upload the folder with files with large sizes (say > 500MB) , in this case you can use RPC , actually the point is that you should really stream the files so you dont run out of memory (by not using byte[] to get whole file at once). You can find detail disscussion on this with code example at http://geek.hubkey.com/2007/11/upload-file-to-sharepoint-document.html
http://razi.spaces.live.com/- Edited byRazi bin RaisMVPWednesday, November 04, 2009 9:19 AMformatting lost
- Marked As Answer byCharlie WuModeratorFriday, November 06, 2009 12:55 AM
All Replies
Doing so by Standard-SP-API would be starting with a FileDirectory and reading all Files and all SubFolders, adding them to SP goes like that:
SPFolder docLib =web.Folders["MyDocLibName"];<br/>//Create File Stream fileContent = new FileStream("c:\MyDocument.doc", FileMode.Open, FileAccess.Read); <br/>SPFile newFile= docLib.Files.Add("MyDopcument.doc",fileContent); newFile.Update();<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>//Create Folders SPFolder newFolder = docLib.SubFolder.Add("NewFolder");<br/>newFolder.Update();<br/><br/>
You could this even do by an RPC-Put (so your Code must not be executed on SP-Server!)
Usage: UploadFileToDocumentLibrary(@”C:\test.txt”, @”http://machine/Shared Documents/textfile.txt”);public static bool UploadFileToDocumentLibrary(string sourceFilePath, string targetDocumentLibraryPath) { //Flag to indicate whether file was uploaded successfuly or not bool isUploaded = true; try { // Create a PUT Web request to upload the file. WebRequest request = WebRequest.Create(targetDocumentLibraryPath); //Set credentials of the current security context request.Credentials = CredentialCache.DefaultCredentials; request.Method = “PUT”; // Create buffer to transfer file byte[] fileBuffer = new byte[8192]; using (Stream stream = request.GetRequestStream()) { using (FileStream fsWorkbook = File.Open(sourceFilePath, FileMode.Open, FileAccess.Read)) { //Get the start point int startBuffer = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length); for (int i = startBuffer; i > 0; i = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length)) stream.Write(fileBuffer, 0, i); } } // Perform the PUT request WebResponse response = request.GetResponse(); //Close response response.Close(); } catch (Exception ex) { isUploaded = false; } return isUploaded; }- Proposed As Answer byMarkus I_ Wednesday, November 04, 2009 10:58 AM
- Marked As Answer byCharlie WuModeratorFriday, November 06, 2009 12:55 AM
- You can use the code available at codeplex for the import/export document utility called "SPIEFolder" http://www.codeplex.com/SPIEFolder. Its a simple console app so you might want to run it first to see the results , then open the code in visual studio. All the code is quite basic so I guess you can read it and get the understanding.Note on uplaoding huge files (e.g > 500MB)You may also want to look into other ways to upload the folder with files with large sizes (say > 500MB) , in this case you can use RPC , actually the point is that you should really stream the files so you dont run out of memory (by not using byte[] to get whole file at once). You can find detail disscussion on this with code example at http://geek.hubkey.com/2007/11/upload-file-to-sharepoint-document.html
http://razi.spaces.live.com/- Edited byRazi bin RaisMVPWednesday, November 04, 2009 9:19 AMformatting lost
- Marked As Answer byCharlie WuModeratorFriday, November 06, 2009 12:55 AM
- Thanks Markus.
I can understand your answer. Anyway I need to create a recursive function ,which will create folders and files from local system to document library.
Samarendra Swain Team Sharepoint www.manuhsolutions.com - Samarendra,
Please have a look at the similar thread
Best Regards, G Vijai Kumar | My Sharepoint Blog - Thanks Razi.
Samarendra Swain Team Sharepoint www.manuhsolutions.com

