Bogdan Mocanu
Bogdan Mocanu

Reputation: 31

error when adding a member to a group in Microsoft graph

I am trying to create one group and add a member to that group in Microsoft Graph, using the REST API. My group is created correctly and OK, but when I add a member to the group, I get back from the server the following exception:

An unexpected 'EndOfInput' node was found when reading from the JSON reader. A 'StartObject' node was expected.

I am trying to add an owner / member (both returns same exception) using the following code:

$graphService.postToGraph("/groups", groupData).then((group) => {
    $graphService.getFromGraph("/me").then((me) => {
        $graphService.postToGraph("/groups/{0}/owners/$ref".format(group.id), me).then((owner) => {
               console.log(owner);
            })
          });
    }

The service is doing the request this way:

 $.ajax({
      type: "POST",
      url: pathToGraph,
      contentType: "application/json",
      headers: {
        'Authorization': 'Bearer ' + token,
      },
      data: JSON.stringify(data)
    })

The creation of the group works well (and same code is use for lots of working requests from Graph), but have a problem with assigning a member. As body, I write the whole json which comes back from "/me". I used the following references to implement this:

Thanks!

Upvotes: 1

Views: 3053

Answers (1)

Bogdan Mocanu
Bogdan Mocanu

Reputation: 31

So, I have found the issue. Apparently there is a small problem in the documentation:

In the request body, supply a JSON representation of user object to be added.

where the link goes to: https://graph.microsoft.io/en-us/docs/api-reference/v1.0/resources/user.htm In order to make it work, you should not put inside a representation of what comes from a user, really needs to create the json string with that user ID and the url to the graph inside:

{"@odata.id": "https://graph.microsoft.com/v1.0/users/<id>"}

After sending this, then the member is added to the group and all works well.

Upvotes: 2

Related Questions