I’m creating an API with ASP.NET Core that accesses a list/drive with the graph api and at some point I need to update an item that was created in the drive library to add an ID (which is a custom column), but I’m getting an invalid request error, can anyone help me?
public async Task<DriveItem?> CreateDriveItem(FileDTO file)
{
var fileBytes = Convert.FromBase64String(file.Base64);
var fileNameWithExtension = file.FileName + file.FileType;
var requestBody = new CreateUploadSessionPostRequestBody
{
Item = new DriveItemUploadableProperties
{
Name = fileNameWithExtension,
}
};
var uploadSession = await _graphClient.Graph.Drives[_driveId].Root.ItemWithPath(fileNameWithExtension).CreateUploadSession.PostAsync(requestBody);
if (uploadSession == null) { return null; }
using var stream = new MemoryStream(fileBytes);
await UploadFileInChunks(uploadSession.UploadUrl ?? "", stream);
var createdItem = await _graphClient.Graph.Drives[_driveId].Root.ItemWithPath(fileNameWithExtension).GetAsync();
if (createdItem == null) { return null; }
var requestBodyPatch = new DriveItem
{
AdditionalData = new Dictionary<string, object>
{
{ "ID_Solicitacao", 1 },
}
};
var patch = await _graphClient.Graph.Drives[_driveId].Items[createdItem.Id].PatchAsync(requestBodyPatch);
if (patch == null) { return null; }
return patch;
}
and the error is:
Microsoft.Graph.Models.ODataErrors.ODataError: Invalid request
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.ThrowIfFailedResponse(HttpResponseMessage response, Dictionary`2 errorMapping, Activity activityForAttributes, CancellationToken cancellationToken)
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory`1 factory, Dictionary`2 errorMapping, CancellationToken cancellationToken)
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory`1 factory, Dictionary`2 errorMapping, CancellationToken cancellationToken)
at Microsoft.Graph.Drives.Item.Items.Item.DriveItemItemRequestBuilder.PatchAsync(DriveItem body, Action`1 requestConfiguration, CancellationToken cancellationToken)
at ApiGraph.src.application.services.SharepointService.CreateDriveItem(FileDTO file) in C:UsersBrunoBispoGiacomelliDocumentsEstudows-aspnetApiGraphsrcapplicationservicesSharepointService.cs:line 227
at ApiGraph.src.adapters.Controllers.SharepointController.CreateDriveItem(FileDTO file) in C:UsersBrunoBispoGiacomelliDocumentsEstudows-aspnetApiGraphsrcadapterscontrollersSharepointController.cs:line 89
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
HEADERS
=======
Accept: application/json, text/plain, */*
Connection: close
Host: localhost:5072
User-Agent: axios/1.5.1
Accept-Encoding: gzip, compress, deflate, br
Content-Type: application/json
Content-Length: 74
request-start-time: 1717675009066