Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I would like to know what are benefits/drawbacks when using either of these methods for downloading a document from Sharepoint

using Microsoft sharepoint library

public void Download(string serverFilePath, string destPath) {
        CreateDirectoryIfNotExists(Path.GetDirectoryName(destPath));
        using (FileInformation ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(_clientCtx, serverFilePath)) {
            using (Stream destFile = System.IO.File.OpenWrite(destPath)) {
                byte[] buffer = new byte[8 * 1024];
                int len;
                while ((len = ffl.Stream.Read(buffer, 0, buffer.Length)) > 0) {
                    destFile.Write(buffer, 0, len);
                }
            }
        }
    }

as oposed to downloading it using WebRequest

public void DownloadFile(string serverFilePath, string destPath) {
        var url = string.Format("{0}/{1}", ServerURL, serverFilePath);
        CreateDirectoryIfNotExists(Path.GetDirectoryName(destPath));
        var request = System.Net.HttpWebRequest.Create(url);
        request.Credentials = _clientCtx.Credentials;
        using (var sr = new StreamReader(request.GetResponse().GetResponseStream())) {
            using (var sw = new StreamWriter(destPath)) {
                sw.Write(sr.ReadToEnd());
            }
        }
    }

because using the HttpRequest results in approximately 30% better performance.

Thanks in advance.

share|improve this question

migrated from stackoverflow.com Mar 7 '13 at 13:35

This question came from our site for professional and enthusiast programmers.

2 Answers 2

up vote 3 down vote accepted

Do you want fries metadata with your file? Use the SharePoint Library.

Only interested in the binary stream of the file? Use the framework code (WebRequest).

share|improve this answer

On top of better performance, one wants to consider what can be most easily read and explained. If you are working on a team, does that team have more familiarity with the SharePoint Library or the WebRequest. Lastly, which of the code snippets could be more module and used in other classes and projects? Taking all of these into consideration will help you determine what is the "best" code.

Hope this helps!

share|improve this answer
    
thank you for your reply. maybe I should have been more clear with my question, i there any specific difference when using Sharepoint method for getting the stream as opposed to just simply getting it through a simple web request. –  Miran Mar 7 '13 at 13:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.