SharePoint Products TechCenter > SharePoint Products and Technologies Forums > SharePoint - Development and Programming > Upload folder from the local system to a document library programmatically ?
Ask a questionAsk a question
 

AnswerUpload folder from the local system to a document library programmatically ?

  • Wednesday, November 04, 2009 7:35 AMsamarendra swain Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Wednesday, November 04, 2009 8:36 AMMarkus I_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    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;
    
        }
    
    

     

  • Wednesday, November 04, 2009 9:16 AMRazi bin RaisMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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/

All Replies

  • Wednesday, November 04, 2009 8:36 AMMarkus I_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    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;
    
        }
    
    

     

  • Wednesday, November 04, 2009 9:16 AMRazi bin RaisMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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/
  • Wednesday, November 04, 2009 9:18 AMsamarendra swain Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Wednesday, November 04, 2009 9:32 AMSP RAJ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Samarendra,

    Please have a look at the similar thread
    Best Regards, G Vijai Kumar | My Sharepoint Blog
  • Wednesday, November 04, 2009 9:38 AMsamarendra swain Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Razi.
    Samarendra Swain Team Sharepoint www.manuhsolutions.com