Udostępnij za pośrednictwem


Downloading files from document library remotely without any OM code

I’ve had a few scenarios where I needed to download files from a document library remotely without using any object model code. Here is simple program that does just that.

First step is use the Lists web service to retrieve list items from the document library

    1: public void DownloadFilesFromDocumentLibrary(string documentLibraryName, string url)
    2:    {
    3:        listservice.Lists proxy = new DownloadFilesFromDocumentLibrary.listservice.Lists();
    4:        //set url and credentials on proxy
    5:        proxy.Url = url + "/_vti_bin/lists.asmx";
    6:        proxy.Credentials = CredentialCache.DefaultCredentials;
    7:        
    8:        //call get list items to retrieve document library items
    9:        XmlNode elm = proxy.GetListItems(documentLibraryName, null, null, null, null, null, null);
   10:        XmlNamespaceManager nsMgr = new XmlNamespaceManager(elm.OwnerDocument.NameTable);
   11:        nsMgr.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
   12:        nsMgr.AddNamespace("z", "#RowsetSchema");
   13:     
   14:        if (elm != null)
   15:        {
   16:            XmlNode dataNode = elm.SelectSingleNode("rs:data", nsMgr);
   17:            int itemCount = Convert.ToInt32(dataNode.Attributes.GetNamedItem("ItemCount").Value);
   18:            if (itemCount == 0)
   19:            {
   20:                Console.WriteLine("Nothing to download");
   21:            }
   22:            else
   23:            {
   24:                                    
   25:                foreach (XmlNode row in dataNode.SelectNodes("z:row", nsMgr))
   26:                {
   27:                    DownLoadFile(documentLibraryName, row.Attributes.GetNamedItem("ows_LinkFilename").Value, url);
   28:                }
   29:            }
   30:        }
   31:   }

In the function above once the list items are retrieved, code loops through each item and calls a method “DownLoadFile” which downloads the specific file from the document library

    1: public void DownLoadFile(string documentLibraryName, string fileName, string url)
    2:     {
    3:         string downloadPage = url + "/_layouts/download.aspx" ;
    4:      
    5:         HttpWebRequest request = null;
    6:         HttpWebResponse response = null;
    7:         Stream responseStream = null;
    8:         FileStream fs = null;
    9:         try
   10:        {
   11:            request = (HttpWebRequest) HttpWebRequest.Create(downloadPage + "?SourceUrl=” + documentLibraryName + ”/" + fileName);
   12:            request.Credentials = CredentialCache.DefaultCredentials;
   13:            request.Timeout = 6000;
   14:            response = (HttpWebResponse)request.GetResponse();
   15:            responseStream = response.GetResponseStream();
   16:     
   17:            //write to filesystem
   18:            fs = new FileStream(fileName, FileMode.Create);
   19:            byte[] buffer = new byte[1024];
   20:            int count = responseStream.Read(buffer, 0, buffer.Length);
   21:            while (count > 0)
   22:            {
   23:                fs.Write(buffer, 0, buffer.Length);
   24:                count = responseStream.Read(buffer, 0, buffer.Length);
   25:            }
   26:        }
   27:        finally
   28:        {
   29:            //close
   30:            
   31:            if(fs != null)
   32:                fs.Close();
   33:            if(responseStream != null)
   34:                responseStream.Close();
   35:            if(response!=null) 
   36:                response.Close();
   37:        }
   38:    }

Comments

  • Anonymous
    February 08, 2009
    PingBack from http://www.clickandsolve.com/?p=4853

  • Anonymous
    February 08, 2009
    Hi, many thanks for your guidance. Very useful for me. Keep great job posted.

  • Anonymous
    January 27, 2010
    int method DownLoadFile fs.Write(buffer, 0, buffer.Length); should be fs.Write(buffer, 0, count);