OpenBinaryStream does not exist SharePoint file download using CSOM C#

g h 716 Reputation points
2020-09-04T02:19:15+00:00

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:

        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();
        }
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,608 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jerryzy 10,571 Reputation points
    2020-09-04T08:12:12.173+00:00

    Try to use FileInformation class to get the file stream:

                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);
    
                using (var memoryStream = new MemoryStream())
                {
                    fileInfo.Stream.CopyTo(memoryStream);
                    return memoryStream.ToArray();
                }
    

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


    If an Answer is helpful, please click "Accept Answer" and upvote it.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.