0

Here is the code I am using to add users to the SharePoint group from rest API

$.ajax({
  url: siteUrl + "/_api/web/sitegroups(" + groupid + ")/users",
  method: "POST",
  body: JSON.stringify({
    '__metadata': {
      'type': 'SP.User'
    },
    'LoginName': uloginname
  }),
  headers: {
    "accept": "application/json; odata=verbose",
    "X-RequestDigest": $('#__REQUESTDIGEST').val(),
    "content-type": "application/json; odata=verbose",

  },
  success: function(data) {
    $("#result").append("New User:<b>" + email + "</b> added successfully to,\n  SharePoint Group: " + groupname);
    $("#cont").hide();
    updateitem(id, email, "approve");
    
  },
  error: function(err) {
   
    $("#result").append("error occured: " + JSON.stringify(err));
  }
});

Here is the error I receive

error occured: {"readyState":4,"responseText":"{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"A node of type 'EndOfInput' was read from the JSON reader when trying to read the start of an entry. A 'StartObject' node was expected."}}}","responseJSON":{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"A node of type 'EndOfInput' was read from the JSON reader when trying to read the start of an entry. A 'StartObject' node was expected."}}},"status":400,"statusText":"Bad Request"}

user388969
  • 337
  • 1
  • 9
  • 30

2 Answers2

0

Try replacing body: JSON.stringify() with data: JSON.stringify()

Michael Han
  • 3,475
  • 1
  • 6
  • 8
  • doesn't work, it worked when I put "X-RequestDigest": $('#__REQUESTDIGEST').val(), at the end instead of second line. But still, all the users who are in the full control group can't add users, they all can add manually going to the site but this code gives them this error all the time. once Site collection admins are able. all the users have full control and are owners in the groups. Not sure what is the problem. – user388969 Aug 19 '21 at 13:18
0

Try using below code. I just tried this at my end and it is working for me:

<script type="text/javascript" src="/sites/siteName/SiteAssets/jquery.min.js"></script>

<button type="button" onclick="addUserToGroup()"> Add User To Group </button>

<script type="text/javascript">
    function addUserToGroup() {
        var addUserToGroupEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups(38)/users";

        var payload = JSON.stringify({
            '__metadata': { 'type': 'SP.User' },
            'LoginName': 'i:0#.f|membership|meganb@tenant.onmicrosoft.com'
        });

        $.ajax({
            url: addUserToGroupEndpoint,
            type: "POST",
            data: payload,
            headers: {
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose"
            },
            success: function (data) {
                console.log("User Added to Group successfully!");
            },
            error: function (error) {
                console.log(error);
            }
        });
    }
</script>

Verify and use correct user LoginName in payload and group ID from your SharePoint site in REST endpoint.

Similar thread: REST Call to Sharepoint Online to add user to a site group not working. Either user not unique or Value not in range

Ganesh Sanap
  • 1,386
  • 1
  • 8
  • 18