Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am creating an AutoHosted App in SP 2013 and trying to access the file under one folder of the remote Web (need to pick the file from current project directory location and pass that file to another function).

When I hit the debugging point, I am getting error of:

Cannot invoke HTTP DAV request. There is a pending query.

I am using below code to open the file (Folder1 is the folder and abc.txt is the file which I need to pick):

Creation of context:

// The following code gets the client context and Title property by using TokenHelper.
// To access other properties, you may need to request permissions on the host web.

var contextToken = TokenHelper.GetContextTokenFromRequest(Page.Request);
var hostWeb = Page.Request["SPHostUrl"];

using (var clientContext = TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken, Request.Url.Authority))
    {
    clientContext.Load(clientContext.Web);
    clientContext.ExecuteQuery();
    Response.Write(clientContext.Web.Title);

Accessing the file:

Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext , "/Folder1/abc.txt")

Currently I am calling this from the aspx.cs file of the remote web. I have given the full control to Web and Site Collection in AppManifest. Is there any way I can get the access to this file?

Thanks in advance!

share|improve this question
    
Hope you've figured out the solution. Please share if you've, i'm also facing the same error. I'm trying to read a file from a document library and then upload it to google drive. –  AshishRawat Feb 21 at 13:57

2 Answers 2

I got a similar error when doing this in one of our Provider-hosted apps, what I did was to create a new ClientContext without the appropriate credentials, which actually worked. Could be a security bug, but you could give this a try:

// ClientContext clientContext (your original ClientContext)
// string fileRef (Ref for the file you want to retrieve)

using (var clientContextHack = new ClientContext(clientContext.Url)) {
    var file = File.OpenBinaryDirect(clientContextHack, fileRef);
}

Update:

Just found out that using OpenBinaryStream works quite well, at least in my Provider-hosted app. I just had problems finding out how to use it properly, and as far as I have seen I must call OpenBinaryStream followed by ExecuteQuery.
Short example of how I do this in my MVC5 app (was not able to return data.Value directly because of Dispose):

var file = clientContext.Web.GetFileByServerRelativeUrl(fileRef);
var data = file.OpenBinaryStream();
clientContext.ExecuteQuery();

byte[] dataBytes;
using (var stream = new MemoryStream())
{
    data.Value.CopyTo(stream);
    dataBytes = stream.ToArray();
}

return new FileContentResult(dataBytes, "image/jpeg");
share|improve this answer

You should try accessing the file by its path in your App.

e.g. ~/Folder1/abc.txt

Or use the Server.MapPath() method.

Btw, you can also use the System.IO namespace (e.g. StreamReader) to read a file on and from the remote web.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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