I have a PDF I need to upload to a SharePoint Document Library using Microsoft Graph. My code has successfully done this for months, until it stopped working April 17 with seemingly no change to our SharePoint and certainly not my code.
I have a function that performs the upload:
public static async Task<DriveItem> UploadLargeFile(string localFilePath, string spPath)
{
DriveItem uploadedFile = null;
using (Stream fileStream = System.IO.File.OpenRead(localFilePath))
{
UploadSession uploadSession = await GraphAPIConnection.GraphClient.Sites[siteid].Drives[driveid].Root.ItemWithPath(spPath).CreateUploadSession().Request().PostAsync();
List<Exception> exceptions = new List<Exception>();
if (uploadSession != null)
{
//Chunk size must be divisible by 320KiB, our chunk size will be slightly more than 1MB
int maxSizeChunk = (320 * 1024) * 4;
ChunkedUploadProvider uploadProvider = new ChunkedUploadProvider(uploadSession, GraphAPIConnection.GraphClient, fileStream, maxSizeChunk);
IEnumerable<UploadChunkRequest> chunkRequests = uploadProvider.GetUploadChunkRequests();
byte[] readBuffer = new byte[maxSizeChunk];
//Using one session, upload each chunk of the file individually
foreach (UploadChunkRequest request in chunkRequests)
{
try
{
UploadChunkResult result = await uploadProvider.GetChunkRequestResponseAsync(request, readBuffer, exceptions);
if (result.UploadSucceeded)
{
uploadedFile = result.ItemResponse;
}
}
catch (Exception ex)
{
Debug.WriteLine("message: " + ex.Message + "\t\ttrace: " + ex.StackTrace);
}
}
}
}
return uploadedFile;
}
My catch produces the following output:
message: Code: generalException Message: An error occurred sending the request. trace: at Microsoft.Graph.HttpProvider.d__19.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.HttpProvider.d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.UploadChunkRequest.d__17.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.UploadChunkRequest.d__16.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.ChunkedUploadProvider.d__17.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at MyNameSpace.SharePointUploader.d__9.MoveNext() in MyDir\SharePointUploader.cs:line 173
As you can see,
uploadProvider.GetChunkRequestResponseAsync(request, readBuffer, exceptions)
leads to the following that produces an error:
at Microsoft.Graph.HttpProvider.d__19.MoveNext()
I've double checked that all of the inputs to the function appear valid; and again there have been no changes to the code in months. I've tried a third party ETL software and was able to upload the PDF to the folder I wanted - that software is also built using Microsoft Graph.
Here is where I am stumped. How can I further debug this to determine exactly why HttpProvider is throwing an error?