Answered How to download files in document library with the help of code

  • Monday, April 16, 2012 4:44 AM
     
     

    Hi Friends

    I am Sidhanta Tripathy. I have a document library. In document library there are some files and folders. I want to download all the files except folders simultaneously. Along with the files in the folders of document library with code.

    Please share your valuable ideas.


    With Thanks

    Sidhanta Tripathy


    • Edited by Sidhanta Monday, April 16, 2012 5:26 AM
    •  

All Replies

  • Monday, April 16, 2012 5:22 AM
     
      Has Code

    You can get the SPFile object and then write it using FileStream. Below is the code snippet -

    SPFile file = web.GetFolder("Documents").Files[fileName];
    //SPFile file = item.ParentList.ParentWeb.GetFile(item.Attachments.UrlPrefix +  fileName); ... get attachments
    FileStream outStream = new FileStream(localFileName, FileMode.Create);
    byte[] fileData = file.OpenBinary(); 
    outStream.Write(fileData, 0, fileData.Count()); 
    outStream.Close(); 
    fileName - name of file in document library.

    "Documents" - name of the document lib

    localFileName - file name on the local drive


    ArchaHim

  • Monday, April 16, 2012 5:23 AM
     
     
  • Monday, April 16, 2012 5:26 AM
     
     

    Hi ArchaHim

    Thanks for your valuable suggestion.But I want to download all the files simultaneously from the document library and all the files from the folders which resides in that documents library.


    With Thanks

    Sidhanta Tripathy


    • Edited by Sidhanta Monday, April 16, 2012 5:26 AM
    •  
  • Monday, April 16, 2012 5:28 AM
     
     

    Hi  Mr. Milan Chauhan

    Thanks a lot for helping me. The code is for sharepoint 2007. Is this working for sps2010.


    With Thanks

    Sidhanta Tripathy



    • Edited by Sidhanta Monday, April 16, 2012 5:33 AM
    •  
  • Monday, April 16, 2012 5:30 AM
     
     
    It seems my article http://sharepointdragons.com/2011/10/07/parallel-programming-in-sharepoint-2010-the-back-to-the-future-pattern/ does exactly what you need. You need to leverage the Task Parallel Library (TPL) to accomplish this. Read the article and do something like it. You have one advantage, as you're only downloading stuff, you won't need to use .NET 3.5 (to call the SharePoint object model) and wrap it in .NET 4 code like I needed to do in the article.

    Kind regards,
    Margriet Bruggeman

    Lois & Clark IT Services
    web site: http://www.loisandclark.eu
    blog: http://www.sharepointdragons.com


  • Monday, April 16, 2012 5:35 AM
     
     Answered Has Code
    Here is the complete code -
    SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists.TryGetList(DOC_LIB);
    SPFolderCollection folderColl = docLib.RootFolder.SubFolders;
    IEnumerator folders = folderColl.GetEnumerator();
    while (folders.MoveNext() != false)
    {
    	folder = (SPFolder)folders.Current;
    	SPFileCollection fileColl = folder.Files;
            IEnumerator files = fileColl.GetEnumerator();
            while (files.MoveNext() != false)
            {
                  SPFile file = (SPFile)files.Current;
    	      FileStream outStream = new FileStream(localFileName, FileMode.Create);
    	      byte[] fileData = file.OpenBinary(); 
                  outStream.Write(fileData, 0, fileData.Count()); 
                  outStream.Close();  
            }
    }
    Hope this helps.

    ArchaHim

    • Proposed As Answer by ArchaHim Monday, April 16, 2012 5:35 AM
    • Marked As Answer by Sidhanta Monday, April 16, 2012 5:37 AM
    •