msgraph-sdk-dotnet icon indicating copy to clipboard operation
msgraph-sdk-dotnet copied to clipboard

[Client bug]: `TableRowCollection: add` is broken in v5?

Open nickwb opened this issue 2 years ago • 9 comments

Describe the bug It's not clear that it is possible to construct a valid instance of Microsoft.Graph.Drives.Item.Items.Item.Workbook.Worksheets.Item.Tables.Item.Rows.Add.AddPostRequestBody.

This HTTP request requires a property "values", that contains nested JSON, which expects "an array of arrays", where each inner array is a complete row to be inserted to the table.

However, it is not clear at all how to construct an instance of the Microsoft.Graph.Models.Json type that is for the AddPostRequestBody.Values property.

The C# code example that is provided on this Microsoft documentation page is clearly nonsense.

Expected behavior TableRowCollection: add should be supported in v5 (it works fine in v4).

Screenshots If applicable, add screenshots to help explain your problem.

Client version 5.11.0

Desktop (please complete the following information):

  • OS: Windows 10

nickwb avatar May 25 '23 03:05 nickwb

Working with workbook is still not supported in v5. This is another issue.

MartinM85 avatar May 25 '23 09:05 MartinM85

i am trying to figure out how write new rows, with this https://learn.microsoft.com/en-us/graph/api/tablerowcollection-add?view=graph-rest-1.0&tabs=csharp but i have a error with the format of the data. The example from mirosoft is confused for me . If in previus version worked can someone share the a example of working code and the exact version Thank´s

JoseCarreira2018 avatar May 28 '23 18:05 JoseCarreira2018

@JoseCarreira2018 - in the v4 SDK, you can try this:

var myDriveId = "xxx";
var myWorkbookPath = "some_folder/workbook.xlsx"
var myTableName = "MyTable"

var table = _graphClient.Drives[myDriveId].Root.ItemWithPath(myWorkbookPath).Workbook.Tables[myTableName];

// In this example: Two rows to insert, each with three columns
var rowsToAdd = JsonDocument.Parse("[[1,2,3][4,5,6]]");

// If you need to build this dynamically, you might find JsonArray in System.Text.Json.Nodes to be helpful

var response = await table.Rows.Add(values: rowsToAdd).Request().PostAsync(CancellationToken.None);
// If this needs to run frequently, you should probably pass a session Id when calling "Request"
// See: CreationSession on the workbook.
// You can pass as: new HeaderOption("Workbook-Session-Id", mySessionId)

This is a very simplified version of code I had working correctly against 4.48.0 of Microsoft.Graph - though I suspect it will work fine with any v4 version.

nickwb avatar May 29 '23 06:05 nickwb

@nickwb Thank´s for the help but i need it work on v5.12

in sample code from microsoft the problem is the format of the data was i want insert. There is another way to load the "Values"/Data to insert?

// Code snippets are only available for the latest version. Current version is 5.x

var graphClient = new GraphServiceClient(requestAdapter);

var requestBody = new Microsoft.Graph.Drives.Item.Items.Item.Workbook.Tables.Item.Rows.Add.AddPostRequestBody { Index = 5, Values = new List<List<1>> { new List<1> { 1, 2, 3, }, new List<4> { 4, 5, 6, }, }, }; var result = await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].Workbook.Tables["{workbookTable-id}"].Rows.Add.PostAsync(requestBody);

JoseCarreira2018 avatar Jun 05 '23 09:06 JoseCarreira2018

@JoseCarreira2018 - It doesn't work on 5.x, that's why I raised this as a bug.

Yes, I've seen that code example too. It doesn't work. It's very clearly a broken example. It's not even syntactically valid C#, let alone something that will pass a type check.

The only way to do it in 5.x is to call _graphClient.RequestAdapter.SendAsync directly.

nickwb avatar Jun 05 '23 11:06 nickwb

I've added corresponding issue to the docs: https://github.com/microsoftgraph/microsoft-graph-docs-contrib/issues/83 Because example is non-sense.

And the whole Json API is dumb. Why it can be only object instead of just literals and arrays. But for some reason it allows to track changes?

How this was created at all? It doesn't do required things, like convert C# structs/classes to JSON, but allow to track changes...

werwolfby avatar Jun 16 '23 16:06 werwolfby

v4 example like this:

var rowsToAdd = JsonDocument.Parse("[[1,2,3][4,5,6]]");

Was stupid too, I have a C# class, that then I have to convert it to JSON string, to parse it back into JsonDocument, to parse it to JSON during sending request. WTF???? Some steps are clearly not needed.

I can create JSON document directly, but why this is not automated.

So this package is not a high-level API is some dumb wrapper 😢.

werwolfby avatar Jun 16 '23 16:06 werwolfby

@andrueastman Is there any ETA when SDK v5 will support workbook endpoints?

MartinM85 avatar Jun 16 '23 19:06 MartinM85

A workaround that work´s using sendasync : ` public async static Task AddInfoToExcel(string accessToken, string name, string address) { string endpoint = "https://graph.microsoft.com/v1.0/users/[email protected]/drive/items/01M45CPBPFYESLZSCWVFDI42ZKKPWQNNPD/workbook/tables/Tabela1/rows/add"; using (var client = new HttpClient()) { using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint)) { // Populate UserInfoRequest object string[] userInfo = { name, address }; string[][] userInfoArray = { userInfo }; UserInfoRequest userInfoRequest = new UserInfoRequest(); userInfoRequest.index = null; userInfoRequest.values = userInfoArray;

            // Serialize the information in the UserInfoRequest object
            string jsonBody = JsonConvert.SerializeObject(userInfoRequest);
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            request.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");

            using (var response = await client.SendAsync(request))
            {
                if (response.IsSuccessStatusCode)
                {
                    return Resource.Graph_UploadToExcel_Success_Result;
                }
                return response.ReasonPhrase;
            }
        }
    }
}

`

JoseCarreira2018 avatar Jun 30 '23 16:06 JoseCarreira2018