DriveItem AdditionalData Patch method with error in Graph Api

Bruno Bispo 0 Reputation points
2024-06-06T17:08:51.2033333+00:00

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?

EDIT: I found the problem, apparently it doesn't recognize the additonal fields, when I put only Name it works. How can I update custom fields?

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:\Users\BrunoBispoGiacomelli\Documents\Estudo\ws-aspnet\ApiGraph\src\application\services\SharepointService.cs:line 227
   at ApiGraph.src.adapters.Controllers.SharepointController.CreateDriveItem(FileDTO file) in C:\Users\BrunoBispoGiacomelli\Documents\Estudo\ws-aspnet\ApiGraph\src\adapters\controllers\SharepointController.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

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,614 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,274 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,509 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
343 questions
{count} votes

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.