Search code examples
c#sdkmicrosoft-graph-sdkskiota

C# Struggling to use MS Graph SDK with Microsoft.Graph.Models.Json


I am trying to modify a SharePoint excel file adding data produced in my program to a spreadsheet hosted on sharepoint. I have been able to successfully add a row to the actual table but not with data.

The problem is with using the Json class from Microsoft.Graph.Models.Json. I have looked extensively around and came up with nothing. I am hoping someone can provide me with a solution/workaround for accessing/using the Json class.

I am trying to use this function provided by the SDK:

var addReq = new WorkbookTableRow
{
    Values = new List<List<Number>>
    {
        new List<Number>
        {
            1,
            2,
            3,
        },
        new List<Number>
        {
            4,
            5,
            6,
        },
    },
};
await GraphClient.Drives[drive.Id].Items[excelFile.Id].Workbook.Worksheets[sheetName].Tables[table.Value[0].Id].Rows.PostAsync(addReq);

Request body pulled straight from: https://learn.microsoft.com/en-us/graph/api/table-post-rows?view=graph-rest-1.0&tabs=csharp A few problems with the code is compiler warnings using the Number class but also changing it to anything (int,double,ect) doesnt work. I expect that the above provided code by Microsoft would add a row to a table with the values listed and not have compiler errors.

I am looking for a workaround and insight on how to do something as basic as adding data to a sheet. This SDK looks like it has had this issue for a year, I know Kiota is generated but this seems pretty trivial and should have been tested by someone?

Edit: I am using the V5 of the SDK and tried the prerelease as well but it was unchanged. Would this be easier to downgrade to V4 or older?


Solution

  • I have been waiting for more than a year for the graph.Json issue to be resolved.

    Currently it's blocked by https://github.com/microsoft/OpenAPI.NET.OData/issues/511 and https://github.com/microsoftgraph/msgraph-metadata/issues/596

    In this case, it seems to me better to wait until the issue is resolved on Microsoft side.

    Update

    In latest version 5.56 of SDK, you can add table rows this way

    var addReq = new WorkbookTableRow
    {
        Values = new UntypedArray(new List<UntypedArray>
        {
            new UntypedArray(new List<UntypedNode>
            {
                new UntypedInteger(1),
                new UntypedInteger(2),
                new UntypedInteger(3)
            }),
            new UntypedArray(new List<UntypedNode>
            {
                new UntypedInteger(4),
                new UntypedInteger(5),
                new UntypedInteger(6)
            })
        })
    }