Search code examples
jquerysharepoint-2013restsharepoint-designer

Not able to add user to SharePoint Group using Rest Api


I have been trying to add users through rest api. It says user doesn't exist or not unique all the time

here is my code

<script type="text/javascript" src="/_layouts/15/SP.RequestExecutor.js"></script>

var executor;
                       executor = new SP.RequestExecutor(siteUrl);
                       executor.executeAsync({
                       url: siteUrl+"/_api/web/sitegroups("+groupid+")/users?@target='"+siteUrl+"'",
                       method: "POST",
                       body: JSON.stringify({ '__metadata': { 'type': 'SP.User' }, 'LoginName':'i:0#.w|domain\\ userloginname' }),
                       headers: {
                       "accept": "application/json; odata=verbose",
                       "content-type": "application/json; odata=verbose"
                       },
                       success:function(data)
                       {
                       alert("New User:"+email+" added successfully in SharePoint Group: "+groupid);
                     
                       },
                       error: function(err)
                       {
                                                                              console.log(JSON.stringify(err));
                       alert("error: " + JSON.stringify(err));
                       }
                       });

my login name is = userfirstname userlastname like (John Stkeas)

this is the error i get all the time

{"responseAvailable":true,"body":"{"error":{"code":"-2130575276, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The user does not exist or is not unique."}}}","statusCode":404,"statusText":"Not Found","contentType":"application/json;odata=verbose;charset=utf-8","allResponseHeaders":"

I tried replacing it with an email address (example.com) and following names as well still don't work

  • i:0#.w|domain\userloginname

This is in SharePoint 2013


Solution

  • The same LoginName i:0#.w|domain\userloginname works for me.

    I would suggest you use the endpoint /_api/web/siteusers?$select=LoginName to check what your login name is.

    enter image description here