0

I m trying to read a file from OneDrive using official SDK and a code from OneDrive API Browser Sample as follows.

var stream = await graphClient.Drives[drive.Id].Items[item.Id].Content.Request().GetAsync();

Where drive.Id and item.Id are valid IDs retrieved by previous successful calls to Graph.

I receive the following error.

An error occurred sending the request. Microsoft.Graph.ServiceException at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)

Another time I got more detailed exception.

at Microsoft.Graph.HttpProvider.d__21.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.HttpProvider.d__20.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.HttpProvider.d__19.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.BaseRequest.d__36.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.BaseRequest.d__34.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

How can I resolve this issue?

1
  • I ve spent a lot of time and started to suspect throttling. But it was hard to decide exactly. Then I gave up and put it aside for 3 days. After a pause, I retried very same code and it worked perfectly. I assume it was throttling. Commented Oct 3, 2021 at 15:53

1 Answer 1

1

Try this -

// get reference to stream of file in OneDrive
GraphServiceClient graphClient = GetAuthenticatedGraphClient(...);
var fileStream = graphClient.Drives[drive.Id].Items[fileId]
                                     .Content
                                     .Request()
                                     .GetAsync()
                                     .Result;

var currentFolder = System.IO.Directory.GetCurrentDirectory();
var driveItemPath = Path.Combine(currentFolder, "proposal.docx");

// save stream to the local file
var driveItemFile = System.IO.File.Create(driveItemPath);
fileStream.Seek(0, SeekOrigin.Begin);
fileStream.CopyTo(driveItemFile);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the suggestion, @Shweta. Funny enough, I started from this. But had request errors. Then I thought that it is due to sync call and rewrote code async. It failed also. I ve spent a lot of time and started to suspect throttling. But it was hard to decide exactly. Then I gave up and put it aside for 3 days. After a pause, I retried very same code and it worked perfectly. I assume it was throttling. I do not mark your suggestion as answer. Your code is correct. My code is also correct. Throttling seems real reason.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.