Monday, May 8, 2017

file.openbinarydirect throws exception sharepoint 2013: Resolved

I was trying download the byte stream from the share point document using share point library in C# MVC application with OpenBinaryDirect method in file class, but it's throwing on exception
file.openbinarydirect throws exception

Code which has an exception:


            Uri filename = new Uri(filePath);
            string server = filename.AbsoluteUri.Replace(filename.AbsolutePath, "");
            string serverrelative = filename.AbsolutePath;

            Microsoft.SharePoint.Client.FileInformation file = Microsoft.SharePoint.Client.File.OpenBinaryDirect(this.ClientContext, serverrelative);
            return file.Stream;
Here we are getting the exception







We are getting the issue because of File.OpenBinaryDirect can not evaluate the app token when you try to get the share point documents with OAuth authorization

Actual Code with resolution is below


            Uri filename = new Uri(filePath);
            string server = filename.AbsoluteUri.Replace(filename.AbsolutePath, "");
            string serverrelative = filename.AbsolutePath;

            Microsoft.SharePoint.Client.File file = this.ClientContext.Web.GetFileByServerRelativeUrl(serverrelative);
            this.ClientContext.Load(file);
            ClientResult<Stream> streamResult = file.OpenBinaryStream();
            this.ClientContext.ExecuteQuery();
            return streamResult.Value;

1 comment:

  1. Does this mean the OpenBinaryStream() will take care of OAuth token refresh as well?

    ReplyDelete