Upload small files to SharePoint Online using batch error

Taupiac Alexandre 0 Reputation points
2023-08-23T14:28:07.8366667+00:00

Hi Team,

I work on an app using the SDK and I would like to upload files from file system to SharePoint Online.

Below is part of my code:

BatchRequestContentCollection batch = new BatchRequestContentCollection(MSGraphService.graphClient, fileShareitems.Count());
using (FileStream fs = File.OpenRead(item.Path))
{

var fileUploadRequest = graphClient.Drives[drive.Id]
    .Root
    .ItemWithPath(uploadInfo[1]).Content.ToPutRequestInformation(fs);
fileUploadRequest.URI = new Uri(fileUploadRequest.URI.OriginalString + "?@microsoft.graph.conflictBehavior=rename");

var result = await batch.AddBatchRequestStepAsync(fileUploadRequest);

var returnedResponse = await MSGraphService.graphClient.Batch.PostAsync(batch);

var fileResponse = await returnedResponse.GetResponseByIdAsync<DriveItem>(result);
}

I expect to have my files saved to the right document library but I get the following issue:

'e' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0.

at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan1 bytes) at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte marker) at System.Text.Json.Utf8JsonReader.ReadFirstToken(Byte first) at System.Text.Json.Utf8JsonReader.ReadSingleSegment() at System.Text.Json.Utf8JsonReader.Read() at System.Text.Json.JsonDocument.Parse(ReadOnlySpan1 utf8JsonSpan, JsonReaderOptions readerOptions, MetadataDb& database, StackRowStack& stack)

at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory1 utf8Json, JsonReaderOptions readerOptions, Byte[] extraRentedArrayPoolBytes, PooledByteBufferWriter extraPooledByteBufferWriter) at System.Text.Json.JsonDocument.Parse(Stream utf8Json, JsonDocumentOptions options) at Microsoft.Kiota.Serialization.Json.JsonParseNodeFactory.GetRootParseNode(String contentType, Stream content) at Microsoft.Kiota.Abstractions.Serialization.ParseNodeProxyFactory.GetRootParseNode(String contentType, Stream content) at Microsoft.Kiota.Abstractions.Serialization.ParseNodeFactoryRegistry.GetRootParseNode(String contentType, Stream content) at Microsoft.Graph.ResponseHandler1.d__3.MoveNext()

at Microsoft.Graph.ResponseHandler1.<HandleResponseAsync>d__22.MoveNext()

at Microsoft.Graph.BatchResponseContent.d__7`1.MoveNext()

If I try to upload my files without batching by using these samples:

It works well.

Could you please help me pointing what I am doing wrong?

My config:

  • Windows 10
  • Solution WinUI 3 in .NET 6.0
  • MS Graph 5.22.0

Request also posted here: https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/2089

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,781 questions
{count} votes

1 answer

Sort by: Most helpful
  1. TH-4749-MSFT 3,305 Reputation points
    2023-09-08T14:56:44.79+00:00

    Hi Eprach,

    Thanks for the info. Does the issue occur if the file upload query "/drives/<DriveID>/root:/Document.docx:/content?@microsoft.graph.conflictBehavior=replace" is run by itself?

    Thanks.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.