0

we are using the below code to download a file from the SharePoint 2010 on-premise and from SharePoint online site. The code is working fine SharePoint online file and getting the error Method "OpenBinaryStream" does not exist for the SharePoint 2010 on-premise site. Can anyone help me to fix the issue?

 private static byte[] GetFileData(ClientContext clientContext, string SiteUrl, string path)
        {
            try
            {
                var web = clientContext.Web;
                clientContext.Load(web, website => website.ServerRelativeUrl);
                clientContext.ExecuteQuery();
                var regex = new Regex(SiteUrl, RegexOptions.IgnoreCase);
                var siteRelavtiveURL = regex.Replace(path, string.Empty);
                var serverRelativeURL = web.ServerRelativeUrl + siteRelavtiveURL;

                var file = web.GetFileByServerRelativeUrl(serverRelativeURL);
                clientContext.Load(file);
                clientContext.ExecuteQuery();
                var stream = file.OpenBinaryStream();
                clientContext.ExecuteQuery();

                using (var memoryStream = new MemoryStream())
                {
                    stream.Value.CopyTo(memoryStream);
                    return memoryStream.ToArray();
                }
            }
            catch (Exception ex)
            {
               
                return new byte[0];
            }           
        }
1
  • 1
    Hi @TARUN, is there any updates ? Did the code snippet work for you ?
    – Jerry
    Commented Sep 4, 2020 at 6:58

1 Answer 1

1

In SharePoint 2010 CSOM, try to get file stream using Microsoft.SharePoint.Client.File.OpenBinaryDirect, here is the modified code:

 private static byte[] GetFileData(ClientContext clientContext, string SiteUrl, string path)
        {
            try
            {
                var web = clientContext.Web;
                clientContext.Load(web, website => website.ServerRelativeUrl);
                clientContext.ExecuteQuery();
                var regex = new Regex(SiteUrl, RegexOptions.IgnoreCase);
                var siteRelavtiveURL = regex.Replace(path, string.Empty);
                var serverRelativeURL = web.ServerRelativeUrl + siteRelavtiveURL;

                var file = web.GetFileByServerRelativeUrl(serverRelativeURL);
                clientContext.Load(file);
                clientContext.ExecuteQuery();
                FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, serverRelativeURL);
               
                //var stream = file.OpenBinaryStream();
                //clientContext.ExecuteQuery();

                using (var memoryStream = new MemoryStream())
                {
                    fileInfo.Stream.CopyTo(memoryStream);
                    return memoryStream.ToArray();
                }
            }
            catch (Exception ex)
            {

                return new byte[0];
            }
        }

Reference:

Method “OpenBinaryStream” does not exist reading a file from SharePoint 2010

1
  • Thanks for the solution
    – TARUN
    Commented Sep 4, 2020 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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