3

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.

5

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).

0

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!

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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