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

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

  • lunes, 16 de abril de 2012 4:44
     
     

    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 lunes, 16 de abril de 2012 5:26
    •  

Todas las respuestas

  • lunes, 16 de abril de 2012 5:22
     
      Tiene código

    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

  • lunes, 16 de abril de 2012 5:23
     
     
  • lunes, 16 de abril de 2012 5:26
     
     

    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 lunes, 16 de abril de 2012 5:26
    •  
  • lunes, 16 de abril de 2012 5:28
     
     

    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 lunes, 16 de abril de 2012 5:28
    • Editado Sidhanta lunes, 16 de abril de 2012 5:33
    •  
  • lunes, 16 de abril de 2012 5:30
     
     
    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


  • lunes, 16 de abril de 2012 5:35
     
     Respondida Tiene código
    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

    • Propuesto como respuesta ArchaHim lunes, 16 de abril de 2012 5:35
    • Marcado como respuesta Sidhanta lunes, 16 de abril de 2012 5:37
    •