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;
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;