Search code examples
sharepointmicrosoft-graph-apixlsxworksheet

Microsoft Graph add row into sharepoint excel file (The argument is invalid or missing or has an incorrect format)


So I am trying to add a row of data into an excel file via the microsoft graph API.

I was able to successfully set up the authentication and get tokens, read the excel file, etc.

I was also able to run the request to add a new row via Microsoft's graph explorer tool and it worked there perfectly, adding the row via postman and microsoft graph api worked fine.

https://developer.microsoft.com/en-us/graph/graph-explorer

Example request: https://graph.microsoft.com/v1.0/me/drive/items/${ItemId}/workbook/worksheets/Sheet1/tables/${tableId}/rows/add

This worked perfectly from postman and the graph explorer, but when I tried to do this from my localhost (react OR nodejs) it throws "The argument is invalid or missing or has an incorrect format"

Here is my code for the request

const accessToken = await Providers.globalProvider.getAccessToken({ scopes: ['User.Read', 'Files.ReadWrite'] })

const bearer = `Bearer ${accessToken}`;

let headers = new Headers();
headers.append("Authorization", bearer);

const options = {
method: "POST,
headers: headers,
body: {
        "values": [
            [
                "column1", "column2", "column3"
            ]
        ]
    }
};
    
const addResponse = await fetch(SHAREPOINT_URL, secondOptions);

And the error message I receive

{
    "error": {
        "code": "InvalidArgument",
        "message": "The argument is invalid or missing or has an incorrect format.",
        "innerError": {
            "code": "invalidArgument",
            "message": "The argument is invalid or missing or has an incorrect format.",
            "date": "2023-02-16T12:53:56",
            "request-id": "{request-id}",
            "client-request-id": "{client-request-id}"
        }
    }
}

Solution

  • After some thought I realized the only difference between the postman POST request and my react one was the body object.

    The answer was converting the javascript object body to a JSON.stringify(bodyObj). Kind of assumed it would automatically do that, but there it is!

    let bodyObj = {
        values: [
            [
                "1", "2", "3"
            ]
        ]
    }
    
    let options = {
        method: "POST",
        headers: headers,
        body: JSON.stringify(bodyObj),
    };