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

    Pergunta

  • 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


    • Editado Sidhanta segunda-feira, 16 de abril de 2012 05:26
    segunda-feira, 16 de abril de 2012 04:44

Respostas

  • 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

    • Sugerido como Resposta ArchaHim segunda-feira, 16 de abril de 2012 05:35
    • Marcado como Resposta Sidhanta segunda-feira, 16 de abril de 2012 05:37
    segunda-feira, 16 de abril de 2012 05:35

Todas as Respostas

  • 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

    segunda-feira, 16 de abril de 2012 05:22
  • segunda-feira, 16 de abril de 2012 05:23
  • 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


    • Editado Sidhanta segunda-feira, 16 de abril de 2012 05:26
    segunda-feira, 16 de abril de 2012 05:26
  • 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



    • Editado Sidhanta segunda-feira, 16 de abril de 2012 05:33
    segunda-feira, 16 de abril de 2012 05:28
  • 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


    segunda-feira, 16 de abril de 2012 05:30
  • 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

    • Sugerido como Resposta ArchaHim segunda-feira, 16 de abril de 2012 05:35
    • Marcado como Resposta Sidhanta segunda-feira, 16 de abril de 2012 05:37
    segunda-feira, 16 de abril de 2012 05:35