How can I programmatically download a file from sharepoint?

I have a sharepoint site that has an Excel spreadsheet that I need to download based on a schedule.

Is it possible?

+4
source share
4 answers

Why not just use wget.exe <url> . You can put this line in a batch file and run it through the window scheduler.

-2
source

Yes, you can download the file from sharepoint. When you have a URL for a document, it can be downloaded using HttpWebRequest and HttpWebResponse.

with example code

  DownLoadDocument(string strURL, string strFileName) { HttpWebRequest request; HttpWebResponse response = null; request = (HttpWebRequest)WebRequest.Create(strURL); request.Credentials = System.Net.CredentialCache.DefaultCredentials; request.Timeout = 10000; request.AllowWriteStreamBuffering = false; response = (HttpWebResponse)request.GetResponse(); Stream s = response.GetResponseStream(); // Write to disk if (!Directory.Exists(myDownLoads)) { Directory.CreateDirectory(myDownLoads); } string aFilePath = myDownLoads + "\\" + strFileName; FileStream fs = new FileStream(aFilePath, FileMode.Create); byte[] read = new byte[256]; int count = s.Read(read, 0, read.Length); while (count > 0) { fs.Write(read, 0, count); count = s.Read(read, 0, read.Length); } // Close everything fs.Close(); s.Close(); response.Close(); } 

You can also use the GetItem API for the copy service to download the file.

  string aFileUrl = mySiteUrl + strFileName; Copy aCopyService = new Copy(); aCopyService.UseDefaultCredentials = true; byte[] aFileContents = null; FieldInformation[] aFieldInfo; aCopyService.GetItem(aFileUrl, out aFieldInfo, out aFileContents); 

A file can be obtained as an array of bytes.

+5
source

You can also do this:

 try { using (WebClient client = new WebClient()) { client.Credentials = new NetworkCredential("username", "password", "DOMAIN"); client.DownloadFile(http_path, path); } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } 
+2
source

The link to the document in Sharepoint must be a static URL. Use this URL in any solution you need to capture in your schedule.

0
source

All Articles